DEV Community

Cover image for Java Thread Naming and currentThread(): A Practical Guide
Rachit Joshi
Rachit Joshi

Posted on

Java Thread Naming and currentThread(): A Practical Guide

INTRODUCTION

Multithreading is an important part of Java programming when an application needs to perform multiple tasks concurrently. The Thread class provides several methods for working with threads, including methods for naming threads and identifying the thread that is currently executing.

Two useful concepts are getName() and setName() for thread naming, along with the static currentThread() method for obtaining a reference to the currently executing thread.

Why Name a Thread?

When Java creates a thread without an explicitly specified name, it receives an automatically generated name. Giving threads meaningful names can make debugging and monitoring concurrent applications much easier.

For example, instead of seeing a generic thread name, you might see:

Database-Worker
File-Processor
Email-Service

Meaningful names make it easier to understand which thread is performing a particular task.

Getting the Name of a Thread

The getName() method returns the name of a thread.

Syntax
public final String getName()

For example:

class ThreadExample extends Thread {

public void run() {
    System.out.println("Thread is running");
}

public static void main(String[] args) {
    ThreadExample t1 = new ThreadExample();

    System.out.println("Thread name: " + t1.getName());

    t1.start();
}
Enter fullscreen mode Exit fullscreen mode

}

A newly created platform thread receives an automatically generated name unless a name is provided.

Changing the Thread Name

The setName() method allows you to change the name of a thread.

Syntax
public final void setName(String name)

Example:

class NamingExample extends Thread {

public void run() {
    System.out.println("Running...");
}

public static void main(String[] args) {

    NamingExample t1 = new NamingExample();

    System.out.println("Before: " + t1.getName());

    t1.setName("Worker-Thread");

    System.out.println("After: " + t1.getName());

    t1.start();
}
Enter fullscreen mode Exit fullscreen mode

}

The output will show the updated thread name.

Using descriptive names is particularly useful when an application contains many threads.

Naming a Thread During Creation

A thread name can also be supplied when creating a Thread.

For example:

class MyThread extends Thread {

MyThread(String name) {
    super(name);
}

public void run() {
    System.out.println("Executing: " + getName());
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {

public static void main(String[] args) {

    MyThread t1 = new MyThread("Download-Thread");
    MyThread t2 = new MyThread("Upload-Thread");

    t1.start();
    t2.start();
}
Enter fullscreen mode Exit fullscreen mode

}

This approach is convenient when you already know the purpose of the thread when creating it.

What Is currentThread() in Java?

currentThread() is a static method of the Thread class that returns the Thread object representing the thread that is currently executing.

Syntax
public static Thread currentThread()

You can use it like this:

Thread current = Thread.currentThread();

After obtaining the current thread, you can call methods such as getName().

Example of currentThread()
class CurrentThreadExample extends Thread {

public void run() {
    Thread t = Thread.currentThread();

    System.out.println("Current thread: " + t.getName());
}

public static void main(String[] args) {

    CurrentThreadExample t1 = new CurrentThreadExample();
    CurrentThreadExample t2 = new CurrentThreadExample();

    t1.start();
    t2.start();
}
Enter fullscreen mode Exit fullscreen mode

}

Each thread can retrieve a reference to itself using Thread.currentThread().

Combining currentThread() and getName()

A common pattern in Java applications is:

System.out.println(
"Executing thread: " + Thread.currentThread().getName()
);

This is useful for logging and debugging concurrent programs.

For example:

class Task extends Thread {

public void run() {
    System.out.println(
        "Task executed by: " +
        Thread.currentThread().getName()
    );
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {

public static void main(String[] args) {

    Task t1 = new Task();
    t1.setName("Task-Worker");

    t1.start();
}
Enter fullscreen mode Exit fullscreen mode

}

Conclusion

Java provides simple but powerful tools for identifying and managing threads. The getName() method retrieves a thread's name, while setName() allows developers to assign a meaningful name. The Thread.currentThread() method goes one step further by providing a reference to the thread that is currently executing.

Together, these methods are especially useful for debugging, logging, monitoring, and understanding multithreaded Java applications.

Top comments (0)