Java Naming a Thread and Fetching Name of Current Thread

Last Updated : 31 Mar, 2026

In Java, a thread is a lightweight process, and assigning meaningful names to threads improves readability and debugging. By default, threads are named as Thread-0, Thread-1, etc., but we can customize these names.

  • Thread names help in debugging and tracking execution
  • Names can be set during creation or later using methods
  • Improves code clarity in multithreaded applications

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.

1. Using Constructor

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
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();
    }
}

Output
Thread 1: geek1
Thread 2: geek2
Thread is running..
Thread is running..

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 

Java
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());
    }
}

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. 

Syntax: 

public static Thread currentThread()

  • Package: java.lang.Thread
  • Return Type: It returns a reference to the currently executing thread.
Java
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();
    }
}

Output
Fetching current thread name.
Thread-0
Fetching current thread name.
Thread-1
Comment