Thursday, 2 March 2017

Setting and getting thread name and thread ID - Java Program

If you want to set a thread’s name in order to identify a thread that can be done in 2 ways.

  1. Using the constructor of the Thread class
  2. Using the setName() method

Thread ID

Another way to uniquely identify a thread is to get its ID. Thread class has getId() method which returns the thread’s ID.

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.

Setting thread name using constructor of the Thread class

Thread class has a constructor Thread(Runnable target, String name) which takes two arguments, a runnable object and a String which sets the thread’s name.

Example code

 
class MyThread implements Runnable{
    @Override
    public void run() {
     // Getting thread's name
        System.out.println("Thread Name- " +Thread.currentThread().getName());
        // Getting thread's ID
        System.out.println("Thread ID- " +Thread.currentThread().getId() + " For " + Thread.currentThread().getName());
      
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {                
            e.printStackTrace();
        }
        
        System.out.println("Finished with thread");
        
    }
    
}

public class ThreadName {

 public static void main(String[] args) {
      // Creating 3 threads
      Thread t1 = new Thread(new MyThread(), "Thread-1");
      Thread t2 = new Thread(new MyThread(), "Thread-2");
      Thread t3 = new Thread(new MyThread(), "Thread-3");
      // Starting threads
      t1.start();
      t2.start();
      t3.start();
 }
}
 

Output

Thread Name- Thread-2
Thread Name- Thread-3
Thread Name- Thread-1
Thread ID- 12 For Thread-3
Thread ID- 11 For Thread-2
Thread ID- 10 For Thread-1
Finished with thread
Finished with thread
Finished with thread
 

Here it can be seen that thread’s name is set in the constructor and thread’s ID is also retrieved.

Using set() method

 
class MyThread implements Runnable{
    @Override
    public void run() {
     // Getting thread's name
        System.out.println("Thread Name- " +Thread.currentThread().getName());
        // Getting thread's ID
        System.out.println("Thread ID- " +Thread.currentThread().getId() + " For " + Thread.currentThread().getName());
      
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {                
            e.printStackTrace();
        }
        
        System.out.println("Finished with thread");
        
    }
    
}

public class ThreadName {

 public static void main(String[] args) {
   // Creating 3 threads
   Thread t1 = new Thread(new MyThread());
   t1.setName("Thread-1");
      Thread t2 = new Thread(new MyThread());
      t2.setName("Thread-2");
      Thread t3 = new Thread(new MyThread());
      t3.setName("Thread-3");
      // Starting threads
      t1.start();
      t2.start();
      t3.start();

 }

}
 

Output

Thread Name- Thread-1
Thread Name- Thread-3
Thread Name- Thread-2
Thread ID- 12 For Thread-3
Thread ID- 10 For Thread-1
Thread ID- 11 For Thread-2
Finished with thread
Finished with thread
Finished with thread

That's all for this topic Setting and getting thread name and thread ID - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How to run threads in sequence - Java Program
  2. How to create deadlock in Java multi-threading - Java Program
  3. Print odd-even numbers using threads and wait-notify
  4. Producer-Consumer Java program using ArrayBlockingQueue
  5. Race condition in Java multi-threading

You may also like -

>>>Go to Java Programs page

1 comment:

  1. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..

    Base SAS Training in Chennai

    ReplyDelete