Get Thread Name

Thread is the independent path of execution of a thread in a program. The JVM provides an application to execute the multiple thread running concurrently in an application.

Get Thread Name

Thread is the independent path of execution of a thread in a program. The JVM provides an application to execute the multiple thread running concurrently in an application.

Get Thread Name

Get Thread Name

     

Thread is the independent path of execution of a thread in a program. The JVM provides an  application to execute the multiple thread running concurrently in an application.

Understand with Example

In this Tutorial we want to describe you a code that help you in understanding to  get a thread name. For this we have a class name Get Thread Name, Inside the class we have -

1)A GetThreadName implements the Runnable interface, that provides you to execute the thread. An object of GetThreadName is Runnable object.

2)We pass the Runnable object as argument to the Thread constructor that is used to create an object of thread.

3)The Thread object has a Runnable object that implements the run method.

4)Once the thread object is created ,start method is invoked.

5)The start( )return immediately once  a thread has been spawned.

6)th.getName-This method returns you the information about the particular thread running in the code and the information about the specified thread is printed by System.out.println.

7) The thread ends when the run( ) method ends.

On execution the code display you the list of thread executing in a program.

GetThreadName.java

public class GetThreadName implements Runnable{
  
  Thread th;
  public GetThreadName(String name){
  th = new Thread(this,name);
  th.start();  
  }
  
  public void run(){
  System.out.println("Thread name : "+th.getName());
  }
  
  public static void main(String args[]) {
  new GetThreadName("1st Thread");
  new GetThreadName("2nd Thread");
  }
}

Output
Thread name : 1st Thread
Thread name : 2nd Thread
 

Download code