Monday, 5 December 2016

Java Tutorial : Java Threads (How to get the Thread Id)



Click here to watch in Youtube :
https://www.youtube.com/watch?v=i_YcGps9Zqg&list=UUhwKlOVR041tngjerWxVccw

DisplayNumberThread.java
/*
 * public long getId()
 * 
 * Returns the identifier of this Thread. The thread ID
 * is a positive long number generated when this thread
 * was created. The thread ID is unique and remains
 * unchanged during its lifetime. When a thread is
 * terminated, this thread ID may be reused.
 * 
 * Returns: this thread's ID.
 */

class DisplayNumberThread extends Thread
{

    public void run()
    {
        System.out.println("id = " + this.getId() + ", Name = "
                + this.getName());
    }

    public static void main(String args[])
    {
        DisplayNumberThread dnt1 = new DisplayNumberThread();
        DisplayNumberThread dnt2 = new DisplayNumberThread();
        DisplayNumberThread dnt3 = new DisplayNumberThread();
        dnt1.start();
        dnt2.start();
        dnt3.start();
    }
}
Output
id = 11, Name = Thread-1
id = 12, Name = Thread-2
id = 10, Name = Thread-0

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/Thread_Id_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/Thread_Id_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ae6d3e57a6dd46b70df1238cfac12598c07fe0fb/BasicJava/Thread_Id_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment