Java Thread getId Example

In this tutorial we are going to describe about Thread getId () with example.

 
 

Share on Google+Share on Google+

Java Thread getId Example

Ads

In this tutorial we are going to describe about Thread getId () with example.

Java Thread getId Example

In this tutorial we are going to describe about Thread getId () with example.

Thread getId() :

This method returns thread ID of long type. It is positive long number. When your thread is created, it is generated by the JVM. The generated thread id is unique and unchangeable during its lifetime. You can use that id only after thread is terminated.

Example : In this example we are using getId() method to display the thread id.

public class ThreadGetId implements Runnable {

    Thread th;

    public ThreadGetId() {
        th = new Thread(this);
        th.start();
    }

    public void run() {
        //Returns the identifier of this Thread.
        System.out.println(th.getName() + " id : " + th.getId());
    }

    public static void main(String args[]) {
        new ThreadGetId();
        new ThreadGetId();
    }
}

Output :

Thread-0 id : 8
Thread-1 id : 9

Advertisements

Ads

Posted on: November 5, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles