Home Tutorial Java Core Java Thread getId Example

 
 

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

Advertisement

Related Tags for Java Thread getId Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 5, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com