Naming a thread and fetching name of current thread in Java
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 creationimportjava.io.*;// we can create thread by creating the// objects of that classclassThreadNamingextendsThread{ThreadNaming(String name){// call to constructor of// the Thread class.super(name);}@Overridepublicvoidrun(){System.out.println("Thread is running.....");}}classGFG{publicstaticvoidmain (String[] args){// creating two threadsThreadNaming t1 =newThreadNaming("geek1");ThreadNaming t2 =newThreadNaming("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();}}chevron_rightfilter_noneOutput:
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.importjava.io.*;// we can create thread by creating the// objects of that class.classThreadNamingextendsThread{@Overridepublicvoidrun(){System.out.println("Thread is running.....");}}classGFG{publicstaticvoidmain (String[] args){// creating two threadsThreadNaming t1 =newThreadNaming();ThreadNaming t2 =newThreadNaming();// 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());}}chevron_rightfilter_noneOutput:
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.
Recommended Posts:
- Difference between Thread.start() and Thread.run() in Java
- How a thread can interrupt an another thread in Java?
- Main thread in Java
- Thread Pools in Java
- Daemon thread in Java
- Java Thread Priority in Multithreading
- Lifecycle and States of a Thread in Java
- Inter-thread Communication in Java
- Lock framework vs Thread synchronization in Java
- Implement Runnable vs Extend Thread in Java
- Thread Interference and Memory Consistency Errors in Java
- isAlive() and join() methods of Thread Class in Java
- Java.lang.Thread class in Java
- Overloading of Thread class run() method
- Overriding of Thread class start() method




