Java Naming a Thread and Fetching Name of Current Thread
Last Updated : 10 Jan, 2025
A thread can be referred to as a lightweight process. Assigning descriptive names to threads enhances code readability and simplifies debugging. Now let us discuss the different ways to name a thread in Java.
Methods to Set the Thread Name
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 the setName() method of Thread class (Indirect Method)
1. Using Constructor to Set the Thread Name (Direct Method)
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. So, let us check the direct method to set the name of the Thread.
Example: Setting the thread name at the time of creation and also bypassing the thread's name as an argument.
Java // Java Program Illustrating How to Set the name // of Thread at time of Creation // Helper class extending Thread Class class MyThread extends Thread { // Parameterized constructor MyThread(String name){ // Call to constructor of the Thread class // as super keyword refers to parent class super(name); } // run() method for thread @Override public void run(){ System.out.println("Thread is running.."); } } // Driver Class class Geeks { public static void main(String[] args) { // Creating two threads MyThread t1 = new MyThread("geek1"); MyThread t2 = new MyThread("geek2"); // Getting the names of Threads System.out.println("Thread 1: " + t1.getName()); System.out.println("Thread 2: " + t2.getName()); // Starting threads using start() method t1.start(); t2.start(); } }
OutputThread 1: geek1 Thread 2: geek2 Thread is running.. Thread is running..
2. Using setName() method of Thread class (Indirect Method)
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 // Java Program to Get and Change the // Name of a Thread // Helper class extending Thread class class ThreadNaming extends Thread { // override the run method @Override public void run() { System.out.println("Thread is running.."); } } // Main class class Geeks { public static void main(String[] args) { // Creating Threads ThreadNaming t1 = new ThreadNaming(); ThreadNaming t2 = new ThreadNaming(); // Fetching the above created threads names // using getName() method System.out.println("Thread 1: " + t1.getName()); System.out.println("Thread 2: " + t2.getName()); // Starting threads using start() method t1.start(); t2.start(); // Now changing the name of threads t1.setName("geeksforgeeks"); t2.setName("geeksquiz"); // Again getting the new names of threads System.out.println("Thread names after changing" + " the thread names"); // Printing the above names System.out.println("New Thread 1 name: " + t1.getName()); System.out.println("New Thread 2 name: " + t2.getName()); } }
OutputThread 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.
Syntax:
public static Thread currentThread()
- Package: java.lang.Thread
- Return Type: It returns a reference to the currently executing thread.
Example:
Java // Get name of current executing thread // using getName() Method import java.io.*; // Helper class extending to Thread class class ThreadNaming extends Thread { // override run method for the thread @Override public void run() { System.out.println("Fetching current thread name."); // Getting the current thread name // using getname() method System.out.println(Thread.currentThread().getName()); } } // Driver Class class Geeks { public static void main(String[] args) { // Creating two threads inside main() method ThreadNaming t1 = new ThreadNaming(); ThreadNaming t2 = new ThreadNaming(); // Starting threads using start() method which // automatically calls run() method t1.start(); t2.start(); } }
OutputFetching current thread name. Thread-0 Fetching current thread name. Thread-1
Similar Reads
Multithreading in Java Multithreading is a Java feature that allows the concurrent execution of two or more parts of a program for maximum utilization of the CPU. Each part of such a program is called a thread. So, threads are lightweight processes within a process.Different Ways to Create ThreadsThreads can be created by
3 min read
Lifecycle and States of a Thread in Java A thread in Java can exist in any one of the following states at any given time. A thread lies only in one of the shown states at any instant:New StateRunnable StateBlocked StateWaiting StateTimed Waiting StateTerminated StateThe diagram below represents various states of a thread at any instant:Lif
5 min read
Main thread in Java Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.When a Java program starts up, one thread begins running i
4 min read
Java Concurrency - yield(), sleep() and join() Methods In this article, we will learn what is yield(), join(), and sleep() methods in Java and what is the basic difference between these three. First, we will see the basic introduction of all these three methods, and then we compare these three. We can prevent the execution of a thread by using one of th
5 min read
Inter-thread Communication in Java Inter-thread communication in Java is a mechanism in which a thread is paused from running in its critical section, and another thread is allowed to enter (or lock) the same critical section to be executed.Note: Inter-thread communication is also known as Cooperation in Java.What is Polling, and Wha
6 min read
Java Thread Class Thread is a line of execution within a program. Each program can have multiple associated threads. Each thread has a priority which is used by the thread scheduler to determine which thread must run first. Java provides a thread class that has various method calls to manage the behavior of threads b
5 min read
What does start() function do in multithreading in Java? We have discussed that Java threads are typically created using one of the two methods : (1) Extending thread class. (2) Implementing RunnableIn both the approaches, we override the run() function, but we start a thread by calling the start() function. So why don't we directly call the overridden ru
2 min read
Java Thread Priority in Multithreading Java being Object-Oriented works within a Multithreading environment in which the thread scheduler assigns the processor to a thread based on the priority of the thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can either be given by JVM while creatin
5 min read
Joining Threads in Java java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program. If th
3 min read
Java Naming a Thread and Fetching Name of Current Thread A thread can be referred to as a lightweight process. Assigning descriptive names to threads enhances code readability and simplifies debugging. Now let us discuss the different ways to name a thread in Java.Methods to Set the Thread NameThere are two ways by which we can set the name either be it d
4 min read