Naming Threads and the Current Thread

Naveen AnamaneniUpdated · 3 min read

Every thread in Java has a name and an ID. If you don't choose a name, Java uses names such as Thread-0, Thread-1. Meaningful names, such as download-1 or email-sender, make logs and thread dumps much easier to read. The static method Thread.currentThread() returns the thread that is running the current line of code, so any code can find out which thread it's in. In this lesson, you will learn how to name threads and use currentThread().

Methods

MethodDescription
new Thread(task, "name")It is used to create a thread with a name.
setName(String name)It changes a thread's name.
getName()It returns the thread's name.
threadId()It returns the thread's unique ID number (Java 19).
Thread.currentThread()It returns the thread running this code.
Thread.ofPlatform().name("w-", 1)It creates threads named w-1, w-2, ... (Java 21).

Example: Default and Custom Names

Let's see an example of naming and identifying threads.

Names.java▶ Try it
public class Names {
    public static void main(String[] args) throws InterruptedException {
        Runnable report = () -> System.out.println("I am "
            + Thread.currentThread().getName());
 
        System.out.println("main thread is called: "
            + Thread.currentThread().getName());
 
        Thread unnamed = new Thread(report);
        System.out.println("Default name starts with Thread-? "
            + unnamed.getName().startsWith("Thread-"));
 
        Thread named = new Thread(report, "report-navika");
        named.start();
        named.join();
 
        named.setName("finished-report");
        System.out.println("Renamed to: " + named.getName());
 
        Thread.Builder builder = Thread.ofPlatform().name("download-", 1);
        Thread d1 = builder.start(report);
        d1.join();
        Thread d2 = builder.start(report);
        d2.join();
 
        System.out.println("IDs are different? "
            + (d1.threadId() != d2.threadId()));
    }
}
Output
main thread is called: main
Default name starts with Thread-? true
I am report-navika
Renamed to: finished-report
I am download-1
I am download-2
IDs are different? true

Explanation: The program starts in a thread named main. A thread created without a name gets Thread- followed by a number. Named threads report their names through Thread.currentThread(). A thread builder with name("download-", 1) numbers each new thread automatically. Every thread has its own ID.

Names in Logs

Common Mistakes

Relying on default names in logs. Thread-7 tells you nothing; name your threads.

Using names as unique keys. Two threads can share a name; use threadId() to tell them apart.

Using getId(). It's deprecated since Java 19; use threadId().

Practice

  1. Easy: Create three threads named after Navika, Srihitha and Advitha, and have each print its own name.
  2. Harder: Write a helper log(String message) that prints [thread-name] message, and call it from several threads.

Quick check: Thread names

4 questions
1What does Thread.currentThread() return?
2What is the default name pattern for unnamed threads?
3Which method returns a thread's unique ID in modern Java?
4Can two threads have the same name?

Key Takeaways

Was this tutorial helpful?

Lesson 387 of 482