Every Java thread has a name, which we read with getName() and change with setName() or set up front in the Thread constructor or a Thread.Builder. To get the thread name of the code that is running, we call Thread.currentThread().getName().
Thread names have no effect on how threads run, but they appear in log lines, thread dumps, profilers and exception stack traces, so a good name tells us which feature a thread belongs to when something goes wrong.
The following example sets a thread name in three ways and reads it back.
Thread mailer = new Thread(() -> {}, "invoice-mailer"); // name in the constructor
String given = mailer.getName(); // "invoice-mailer"
mailer.setName("receipt-mailer"); // rename at any time
String renamed = mailer.getName(); // "receipt-mailer"
Thread built = Thread.ofPlatform().name("export-", 1).unstarted(() -> {});
String fromBuilder = built.getName(); // "export-1"
String current = Thread.currentThread().getName(); // "main"
Notice that the name is a plain property of the Thread object, so we can set it before or after start(). We look at the default names the JVM and executors generate, the ways to set custom names for single threads and whole pools, the difference between a name and a thread ID, and how names make log files readable.
1. Default Thread Names
When we do not pass a name, Java generates one. The format depends on who creates the thread, and recognizing these formats in a log or a thread dump tells us at once which part of the app started the thread.

| Created by | Default name | Example |
|---|---|---|
| new Thread(task) | Thread- plus a JVM-wide counter | Thread-0, Thread-1 |
| Executors.newFixedThreadPool() and other Executors factories | pool-N-thread-M, N counts pools, M counts threads in the pool | pool-1-thread-1 |
| CompletableFuture.supplyAsync() and parallel streams | Common pool worker | ForkJoinPool.commonPool-worker-1 |
| Thread.ofPlatform() without name() | Same counter as new Thread() | Thread-2 |
| Thread.ofVirtual() and newVirtualThreadPerTaskExecutor() | Empty string | “” |
| The JVM launcher | main | main |
Thread plain = new Thread(() -> {});
boolean counterName = plain.getName().startsWith("Thread-"); // true
Future<String> pooled;
try (ExecutorService pool = Executors.newFixedThreadPool(3)) {
pooled = pool.submit(() -> Thread.currentThread().getName());
}
boolean poolName = pooled.resultNow().matches("pool-\\d+-thread-1"); // true
String common = CompletableFuture.supplyAsync(() -> Thread.currentThread().getName()).join(); // "ForkJoinPool.commonPool-worker-1", the number varies
String virtual = Thread.ofVirtual().unstarted(() -> {}).getName(); // ""
The numbers in Thread-N and pool-N are global counters in the JVM, so the second executor an app creates is pool-2, even if the first one was closed long ago. When logs show pool-7-thread-3, we only know that some executor created it, which is the main reason to give pool threads custom names.
2. Getting the Name of a Thread
The instance method getName() returns the name of any Thread object. Inside a task, we often have no reference to the thread, so we ask for the current one with the static method Thread.currentThread().
AtomicReference<String> seen = new AtomicReference<>();
Thread worker = new Thread(() -> seen.set(Thread.currentThread().getName()), "report-builder");
worker.start();
worker.join();
String inside = seen.get(); // "report-builder"
String outside = worker.getName(); // "report-builder"
The toString() method of a thread also contains the name. For a platform thread, it prints the thread ID, name, priority and thread group, such as Thread[#24,report-builder,5,main], and for a virtual thread it prints something like VirtualThread[#27,vt-1]/runnable.
3. Setting a Custom Thread Name
We can name a single thread in three ways, and all of them can be combined with renaming later through setName().
3.1. Using the Thread Constructor
Several Thread constructors accept the name as the last argument.
- Thread(String name) names a thread whose subclass overrides run().
- Thread(Runnable task, String name) is the usual choice for a lambda or a task class.
- Thread(ThreadGroup group, Runnable task, String name) also places the thread in a thread group, which modern code rarely needs.
Thread billing = new Thread(() -> {}, "billing-sync");
String billingName = billing.getName(); // "billing-sync"
3.2. Using Thread.setName()
The setName() method takes one String argument and returns nothing. It is useful when the thread was created without a name, for example from a lambda or by a library. A null name is rejected.
Thread cleaner = new Thread(() -> {});
cleaner.setName("cache-cleaner");
String cleanerName = cleaner.getName(); // "cache-cleaner"
cleaner.setName(null); // NullPointerException: name cannot be null
3.3. Using Thread.ofPlatform() and Thread.ofVirtual()
The builders added in Java 21 with JEP 444 accept a fixed name, or a prefix and a start number that grows with every thread the builder creates. Builders are the simplest way to name virtual threads, which have no name by default.
Thread.Builder builder = Thread.ofVirtual().name("upload-", 1);
Thread first = builder.unstarted(() -> {});
Thread second = builder.unstarted(() -> {});
String firstName = first.getName(); // "upload-1"
String secondName = second.getName(); // "upload-2"
Thread single = Thread.ofPlatform().name("scheduler").unstarted(() -> {});
String singleName = single.getName(); // "scheduler"
Other ways to create and start threads, including the constructor variants and executors, are compared in a separate guide.
4. Naming the Threads of an ExecutorService
Executors create their threads through a ThreadFactory. To give every pool thread a meaningful name, we pass a factory to the executor. Since Java 21, a builder produces such a factory in one line, so we no longer need a custom class for it.
ThreadFactory pdfThreads = Thread.ofPlatform().name("invoice-pdf-", 1).factory();
Future<String> rendered;
try (ExecutorService pool = Executors.newFixedThreadPool(4, pdfThreads)) {
rendered = pool.submit(() -> Thread.currentThread().getName());
}
String pdfThread = rendered.resultNow(); // "invoice-pdf-1"
ThreadFactory fetchThreads = Thread.ofVirtual().name("fetch-", 1).factory();
Future<String> fetched;
try (ExecutorService executor = Executors.newThreadPerTaskExecutor(fetchThreads)) {
fetched = executor.submit(() -> Thread.currentThread().getName());
}
String fetchThread = fetched.resultNow(); // "fetch-1"
Renaming a pool thread inside a task is a trap, because the new name stays on the thread after the task ends. The next task on the same thread inherits a name that has nothing to do with it.
Future<String> later;
try (ExecutorService single = Executors.newSingleThreadExecutor()) {
single.submit(() -> Thread.currentThread().setName("order-42"));
later = single.submit(() -> Thread.currentThread().getName());
}
String leaked = later.resultNow(); // "order-42", from the previous task
If a task must show its own context in the thread name, it saves the old name and restores it in a finally block. For per-request context in logs, the Mapped Diagnostic Context of the logging framework is the better tool.
static <T> T withThreadName(String name, Callable<T> task) throws Exception {
Thread current = Thread.currentThread();
String original = current.getName();
current.setName(name);
try {
return task.call();
} finally {
current.setName(original);
}
}
String during = withThreadName("order-43", () -> Thread.currentThread().getName()); // "order-43"
String restored = Thread.currentThread().getName(); // "main"
5. Thread Name vs Thread ID
A thread name is a label that we choose, and two threads can share the same name. A thread ID is a positive long that the JVM assigns when it creates the thread, and it stays unique for the life of the JVM and never changes.
Thread a = new Thread(() -> {}, "worker");
Thread b = new Thread(() -> {}, "worker");
boolean sameName = a.getName().equals(b.getName()); // true
boolean sameId = a.threadId() == b.threadId(); // false
Since Java 19, we read the ID with threadId(). The older getId() method is deprecated because a subclass could override it and return a non-unique value, whereas threadId() is final. Thread dumps print the ID after the name, as in “invoice-pdf-1” #41.
6. Thread Names in Log Files
Say a booking app writes logs from web requests, a nightly export job and a pool that sends confirmation emails. With default names, the log shows pool-3-thread-2 and Thread-14, and nobody can tell which line came from the email sender. With named factories and a Logback pattern such as %d %-5level [%thread] %logger{0} – %msg%n, every line carries email-sender-2 or nightly-export.
2026-10-10 09:15:02,114 INFO [http-nio-8080-exec-3] BookingController - booking 42 created
2026-10-10 09:15:02,131 INFO [email-sender-2] ConfirmationMailer - mail queued for booking 42
2026-10-10 09:15:03,877 WARN [email-sender-2] ConfirmationMailer - SMTP retry 1 for booking 42
Logback and Log4j2 print the name with the %thread or %t conversion word, and Spring Boot includes it in its default console pattern. The same names show up in a thread dump, so a stuck email-sender thread points straight at the mail code. The Java concurrency overview links the executor and thread tutorials that build on these names.
7. Thread Name FAQs
Unreadable log lines and anonymous pool threads are why most developers look up thread names in the first place.
7.1. What is the default name of a thread in Java?
A thread created with new Thread() gets Thread- plus a counter, such as Thread-0. Executor threads get pool-N-thread-M, common pool workers get ForkJoinPool.commonPool-worker-N, virtual threads get an empty name, and the first application thread is main.
7.2. Can two threads have the same name?
Yes. Java does not check names for uniqueness, so a factory that always returns the same name creates many threads called the same. Use threadId() when a unique identifier is needed.
7.3. Can we change the name of a thread after it has started?
Yes. The method setName() works on a running thread, and the new name shows up in logs and thread dumps from that point. On pool threads, restore the old name afterwards, as shown in section 4.
7.4. How do we name a virtual thread?
Use Thread.ofVirtual().name(“prefix-“, 1) for a counter or name(“fixed”) for a fixed name, either on the builder or on the factory passed to Executors.newThreadPerTaskExecutor(). Calling setName() on a virtual thread also works.
7.5. Does the main thread always have the name main?
The JVM launcher names it main, but the program can rename it with Thread.currentThread().setName() like any other thread.
8. Conclusion
We read a thread name with getName(), most often through Thread.currentThread(), and set it with the constructor, setName() or a Thread.Builder. Java generates names such as Thread-0 and pool-1-thread-1 when we do not, and virtual threads stay unnamed.
For executors, a builder-made ThreadFactory names every pool thread in one line, and those names make log files and thread dumps readable. Names are not unique and can leak between pool tasks, so we use threadId() for identity and restore names we change inside tasks.
9. References
- Thread (Java SE 25 API)
- Thread.Builder (Java SE 25 API)
- Executors.defaultThreadFactory() (Java SE 25 API)
- JEP 444, Virtual Threads
Happy Learning !!