Naming a thread and fetching name of current thread in Java
Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. now let us discuss the eccentric concept of with what ways we can name a thread.
Methods: There are two ways by which we can set the name either be it directly or indirectly which we will be peeking through.
- Creating the thread and Passing the thread’s name (Direct method)
- Using setName() method of Thread class (Indirect Method)
Method 1: Creating the thread and passing the thread’s name
It is a direct method of naming threads in java, each thread has a name that is: 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.
Geek, now you must be wondering how to set the thread’s name directly? In java we can set the thread name at the time of creating the thread and bypassing the thread’s name as an argument as shown in the below example as follows:
Example:
Java
import java.io.*;
class ThreadNaming extends Thread {
ThreadNaming(String name)
{
super (name);
}
@Override public void run()
{
System.out.println( "Thread is running....." );
}
}
class GFG {
public static void main(String[] args)
{
ThreadNaming t1 = new ThreadNaming( "geek1" );
ThreadNaming t2 = new ThreadNaming( "geek2" );
System.out.println( "Thread 1: " + t1.getName());
System.out.println( "Thread 2: " + t2.getName());
t1.start();
t2.start();
}
}
|
Output
Thread 1: geek1
Thread 2: geek2
Thread is running.....
Thread is running.....
Way 2: Using setName() method of Thread class
We can set(change) the thread’s name by calling the setName method on that thread object. It will change the name of a thread.
Syntax:
public final void setName(String name)
Parameter: A string that specifies the thread name
Example:
Java
import java.io.*;
class ThreadNaming extends Thread {
@Override public void run()
{
System.out.println( "Thread is running....." );
}
}
class GFG {
public static void main(String[] args)
{
ThreadNaming t1 = new ThreadNaming();
ThreadNaming t2 = new ThreadNaming();
System.out.println( "Thread 1: " + t1.getName());
System.out.println( "Thread 2: " + t2.getName());
t1.start();
t2.start();
t1.setName( "geeksforgeeks" );
t2.setName( "geeksquiz" );
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 is running.....
Thread names after changing the thread names
New Thread 1 name: geeksforgeeks
New Thread 2 name: geeksquiz
Thread is running.....
How to fetch the name of the current thread?
Now let us dwell on fetching the name of the current thread. We can fetch the current thread name at the time of creating the thread and bypassing the thread’s name as an argument.
Method: currentThread()
It is defined in java.langThread class.
Return Type: It returns a reference to the currently executing thread
Syntax:
public static Thread currentThread()
Example:
Java
import java.io.*;
class ThreadNaming extends Thread {
@Override public void run()
{
System.out.println(
"Fetching current thread name.." );
System.out.println(
Thread.currentThread().getName());
}
}
class GFG {
public static void main(String[] args)
{
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
Last Updated :
25 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...