Home Tutorial Java Core Java Thread getId Example

 
 

Java Thread getId Example
Posted on: November 5, 2012 at 12:00 AM
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

Related Tags for Java Thread getId Example:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.