Friday, January 1, 2021

Setting And Getting Thread Name And Thread ID in Java

In a multi-threading application if lots of thread have been spawned and you want to identify those thread then you can get the thread's name or thread's ID. This post shows how to set and get thread's name in Java and how to get thread ID in Java.

Setting Thread's name in Java

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 in Java

Another way to uniquely identify a thread is to get thread's ID in Java. 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.

 
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.

Setting thread name using setName() method Java example

 
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 in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Run Threads in Sequence in Java
  2. Print Odd-Even Numbers Using Threads And Semaphore Java Program
  3. How to Create Deadlock in Java
  4. Producer-Consumer Java Program Using ArrayBlockingQueue
  5. Java Multithreading Interview Questions And Answers

You may also like-

  1. Split a String Java Program
  2. Java Program to Find The Longest Palindrome in a Given String
  3. Matrix Multiplication Java Program
  4. Type Erasure in Java Generics
  5. Lambda Expressions in Java 8
  6. Switch Case Statement in Java With Examples
  7. Array in Java With Examples
  8. Covariant Return Type in Java