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
GFG
{
public
static
void
main (String[] args)
{
// creating two threads
ThreadNaming t1 =
new
ThreadNaming(
"geek1"
);
ThreadNaming t2 =
new
ThreadNaming(
"geek2"
);
// 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: geek1 Thread 2: geek2 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
GFG
{
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(
"geeksforgeeks"
);
t2.setName(
"geeksquiz"
);
// 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: geeksforgeeks New Thread 2 name: geeksquiz 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 GFG { 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
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.