Naming Thread and Current Thread in Java

You can give a name to a thread by using setName() method of Thread class. You can also retrieve the name of a thread using getName() method of a Thread class. These two methods are public and final. Below is the method signatures of these methods.

The Thread class provides methods to change and get the name of a thread. By default, each thread has a name i.e. thread-0, thread-1 and so on. By we can change the name of the thread by using setName() method. The syntax of setName() and getName() methods are given below:

1) public final void setName(String name) —-> It changes the name of the thread to “name”.

2) public final String getName() —-> Returns the name of the thread.

Below example shows how to use setName() and getName() methods.
class MyThread extends Thread { @Override public void run() { System.out.println("Keep some task here...."); } } public class Test { public static void main(String[] args) { MyThread thread = new MyThread(); //Creating a thread thread.start(); //Starting a thread thread.setName("My Thread"); //Giving a name to the thread String name = thread.getName(); //Retreiveing the name of the thread System.out.println(name); } }


Output:
My Thread Keep some task here....
Some Things-To-Remember about Naming a thread in java :
  • setName() method may throw a SecurityException at run time if the current thread can not modify the name of the specified thread.

  • You can change the name of a thread at any state of the thread.

class MyThread extends Thread { @Override public void run() { for(int i = 0; i < 51; i++) { System.out.println(i); } } } public class Test { public static void main(String[] args) { MyThread thread = new MyThread(); thread.setName("My Thread"); //Changing the name of the thread before starting the thread thread.start(); thread.setName("My Thread"); //changing the name of the thread when thread is active try { thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } thread.setName("My Thread"); //Changing the name of the thread when thread is sleeping } }


Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Default Name Of The Thread :

In Java, All threads have names. If you are not providing the name to a thread, thread will get default name. Default name of the thread will be consist of a word “Thread”, followed by hyphen (-) and followed by an integer number starting with 0.

public class Test { public static void main(String[] args) { Thread t = new Thread(); System.out.println(t.getName()); } }


Output:
Thread-0
How to retrieve a name of the primary thread or main thread?

First, get the reference of the main thread by using currentThread() method of Thread class. currentThread() method returns the reference of currently executing thread. After, getting the reference of the main thread, use the getName() method to retrieve the name of the thread.

public class Test { public static void main(String[] args) { Thread t = Thread.currentThread(); System.out.println(t.getName()); } }


Output:
main
Can we change the name of the main thread ?

Yes, we can change the name of the main thread. It can be done by first getting the reference of the main thread by using currentThread() method and then calling setName() method on it.

public class Test { public static void main(String[] args) { Thread t = Thread.currentThread(); t.setName("main-thread-name-modified"); System.out.println(t.getName()); } }


Output:
main-thread-name-modified
Another method of naming a thread in java :

You can pass name of the thread while creating the object to thread. There is a constructor in Thread class which takes name of the thread as an argument.

class MyThread extends Thread { public MyThread(String name) { super(name); } @Override public void run() { System.out.println(getName()); //Output : My Thread } } public class Test { public static void main(String[] args) { MyThread t = new MyThread("My Thread"); //passing the name while creating the thread t.start(); } }


Output:
My Thread

Naming a thread is very useful in identifying a thread and also in debugging a code.

Example of naming a thread
class Test extends Thread{ public void run(){ System.out.println("running..."); } public static void main(String args[]){ Test t1=new Test(); Test t2=new Test(); 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...
Current Thread

The currentThread() method returns a reference of currently executing thread.

public static Thread currentThread()
class Test extends Thread{ public void run() { System.out.println(Thread.currentThread().getName()); } public static void main(String args[]) { Test t1=new Test(); Test t2=new Test(); t1.start(); t2.start(); } }


Output:
Thread-0 Thread-1



Instagram