Naming Thread
The Thread class offers functionalities for modifying and retrieving the name associated with a thread. By default, threads are assigned names such as thread-0, thread-1, and so forth. However, it is possible to alter the name of a thread using the setName method. The following are the syntaxes for the setName and getName methods:
- Syntax for setName:
- Syntax for getName:
public final void setName(String name)
public final String getName()
public String getName(): is used to return the name of a thread.
public void setName(String name): is used to change the name of a thread.
It is possible to assign a specific name to a thread during its instantiation by utilizing the constructor of the respective class.
Example of naming a thread : Using setName Method
FileName: TestMultiNaming1.java
class TestMultiNaming1 extends Thread{
public void run(){
System.out.println("running...");
}
public static void main(String args[]){
TestMultiNaming1 t1=new TestMultiNaming1();
TestMultiNaming1 t2=new TestMultiNaming1();
System.out.println("Name of t1:"+t1.getName());
System.out.println("Name of t2:"+t2.getName());
t1.start();
t2.start();
t1.setName("Sonoo Jaiswal");
System.out.println("After changing name of t1:"+t1.getName());
}
}
Output:
Name of t1:Thread-0
Name of t2:Thread-1
After changing name of t1:Sonoo Jaiswal
running...
running...
Example of naming a thread : Without Using setName Method
It is possible to assign a name to a thread during its creation without relying on the setName method. Consider the code snippet below.
FileName: ThreadNamingExample.java
// A Java program that shows how one can
// set the name of a thread at the time
// of creation of the thread
// import statement
import java.io.*;
// The ThreadNameClass is the child class of the class Thread
class ThreadName extends Thread
{
// constructor of the class
ThreadName(String threadName)
{
// invoking the constructor of
// the superclass, which is Thread class.
super(threadName);
}
// overriding the method run()
public void run()
{
System.out.println(" The thread is executing....");
}
}
public class ThreadNamingExample
{
// main method
public static void main (String argvs[])
{
// creating two threads and settting their name
// using the contructor of the class
ThreadName th1 = new ThreadName("C# Tutorial1");
ThreadName th2 = new ThreadName("C# Tutorial2");
// invoking the getName() method to get the names
// of the thread created above
System.out.println("Thread - 1: " + th1.getName());
System.out.println("Thread - 2: " + th2.getName());
// invoking the start() method on both the threads
th1.start();
th2.start();
}
}
Output:
Thread - 1: C# Tutorial1
Thread - 2: C# Tutorial2
The thread is executing....
The thread is executing....
Current Thread
The method currentThread provides a reference to the thread that is currently running.
public static Thread currentThread()
Example of currentThread method
FileName: TestMultiNaming2.java
class TestMultiNaming2 extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[]){
TestMultiNaming2 t1=new TestMultiNaming2();
TestMultiNaming2 t2=new TestMultiNaming2();
t1.start();
t2.start();
}
}
Output:
Thread-0
Thread-1