Naming a Thread
In Java, each thread has a name i.e Thread-0, Thread-1, Thread-2,….so on. Java provides some methods to change the thread name. There are basically two methods to set the thread name. Both methods are defined in java.lang.Thread class.
Setting the thread’s name directly: We can set the thread name at time of creating the thread and by passing the thread’s name as an argument.
// Java program to illustrate
// how to set the name
// of a thread at time of
// thread creation
import java.io.*;
// we can create thread by creating the
// objects of that class
class ThreadNaming extends Thread
{
ThreadNaming(String name)
{
// call to constructor of
// the Thread class.
super(name);
}
@Override
public void run()
{
System.out.println("Thread is running........");
}
}
class PAI
{
public static void main (String[] args)
{
// creating two threads
ThreadNaming t1 = new ThreadNaming("prutor1");
ThreadNaming t2 = new ThreadNaming("prutor2");
// getting the above created threads names.
System.out.println("Thread 1: " + t1.getName());
System.out.println("Thread 2: " + t2.getName());
t1.start();
t2.start();
}
}
Output:
Thread 1: prutor1
Thread 2: prutor2
Thread is running........
Thread is running........
Using setName(String threadName) method: We can set(change) the thread’s name by calling the setName method on that thread object.
Syntax:
public final void setName(String name)
// it will change the name of a thread.
// Java program to illustrate
// how to get and change the
// name of a thread.
import java.io.*;
// we can create thread by creating the
// objects of that class.
class ThreadNaming extends Thread
{
@Override
public void run()
{
System.out.println("Thread is running........");
}
}
class PAI
{
public static void main (String[] args)
{
// creating two threads
ThreadNaming t1 = new ThreadNaming();
ThreadNaming t2 = new ThreadNaming();
// getting the above created threads names.
System.out.println("Thread 1: " + t1.getName());
System.out.println("Thread 2: " + t2.getName());
t1.start();
t2.start();
// Now changing the name of threads.
t1.setName("PrutordotAi");
t2.setName("prutorquiz");
// again getting the new names
// of the threads.
System.out.println("Thread names after changing the "+
"thread names");
System.out.println("New Thread 1 name: " + t1.getName());
System.out.println("New Thread 2 name: " + t2.getName());
}
}
Output:
Thread 1: Thread-0
Thread 2: Thread-1
Thread names after changing the thread names
New Thread 1 name: PrutordotAi
New Thread 2 name: prutorquiz
Thread is running........
Thread is running........
Fetching the name of Current thread
We can fetch the current thread name at time of creating the thread and by passing the thread’s name as an argument.
Syntax:
public static Thread currentThread()
// It is defined in java.langThread class.
// It returns reference to currently executing thread.
// Java program to illustrate
// how to get name of current
// executing thread.
import java.io.*;
// we can create thread by creating the
// objects of that class
class ThreadNaming extends Thread
{
@Override
public void run()
{
// getting the current thread 's name.
System.out.println("Fetching current thread name..");
System.out.println(Thread.currentThread().getName());
}
}
class PAI
{
public static void main (String[] args)
{
// creating two threads
ThreadNaming t1 = new ThreadNaming();
ThreadNaming t2 = new ThreadNaming();
t1.start();
t2.start();
}
}
output:
Fetching current thread name..
Thread-0
Fetching current thread name..
Thread-1