Daemon Threads and Thread Names and IDs
Author: Ter-Petrosyan Hakob
Daemon Threads
In Java, threads can either be user threads or daemon threads. You can make a thread a daemon by using the method:
t.setDaemon(true);
Don’t worry—the name daemon doesn’t mean anything scary! A daemon thread is just a thread that exists to help other threads. For example, you might have:
- A thread that periodically checks for unused files and deletes them.
- A thread that updates a clock in a user interface every second.
Daemon threads are special because the Java program will stop automatically once only daemon threads are left. This makes sense: if there are no important tasks running, there is no need for the program to keep running just for helper threads.
Note: All virtual threads in Java are automatically daemon threads. Trying to call
setDaemon(false)on a virtual thread will not change anything.
Thread Names
Every thread in Java has a name. By default, names look like Thread-1, Thread-2, etc. You can give a thread a meaningful name to make debugging easier:
var t = new Thread(task);
t.setName("DataFetcher");
For example, if your program downloads data from multiple websites at the same time, naming each thread can help you understand which thread is doing what when you look at logs or thread dumps.
To get a thread’s name, you can use:
String name = t.getName();
Thread IDs
Each thread also has a unique ID. This is a number that Java assigns automatically. You can get it using:
long id = t.threadId();
Avoid using the older getId() method because it is deprecated. Someone could override it and return the wrong value. Using threadId() is safer and always gives the correct unique ID.
For instance, if you are tracking multiple threads processing user requests, you could log both the thread name and ID like this:
System.out.println("Thread " + t.getName() + " with ID " + t.threadId() + " started processing.");