Java Data Type Tutorial - Java Thread.getId()








Syntax

Thread.getId() has the following syntax.

public long getId()

Example

In the following code shows how to use Thread.getId() method.

class ThreadDemo implements Runnable {
   public void run() {
      // returns the identifier of this Thread.
      System.out.println("Name = " + Thread.currentThread().getName());
      System.out.print("Id = " + Thread.currentThread().getId());
   }/* w  w  w . java 2s  .  c  om*/
}

public class Main{


   public static void main(String args[]) {

      // thread created
     Thread t = new Thread("java2s.com thread");
      // set thread priority
      t.setPriority(1);
      // prints thread created
      System.out.println("thread  = " + t);
      // this will call run() function
      t.start();
   }

}

The code above generates the following result.