Understanding Daemon and User Threads in Java
This article explains the concepts of daemon and user threads in Java, how to set a thread as daemon or user using Thread.setDaemon, the differences in lifecycle behavior, appropriate use cases, and includes example code demonstrating each type and their impact on JVM termination.
Basic Concepts
In Java there are two kinds of threads: daemon threads and user threads.
Daemon Thread: a special background thread that provides system services such as garbage collection or JIT compilation.
User Thread: the normal work thread that performs the business logic of the program; threads created with new Thread() are user threads by default.
How to Set a Thread as User or Daemon
Call Thread.setDaemon(false) to make it a user thread.
Call Thread.setDaemon(true) to make it a daemon thread.
If you do not set the property, the thread defaults to a user thread.
The daemon flag must be set before the thread is started; otherwise an IllegalThreadStateException is thrown.
Differences Between Daemon and User Threads
User Thread Example
public class UserThread {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
System.out.println("I am a user thread......");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
Thread.sleep(3000);
System.out.println("Main thread finished......");
}
}Running the above code produces the following output (image omitted).
The main thread ends, but the program does not exit because the user thread runs an infinite loop.
Daemon Thread Example
public class UserThread {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
System.out.println("I am a user thread......");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Set as daemon thread
thread.setDaemon(true);
thread.start();
Thread.sleep(3000);
System.out.println("Main thread finished......");
}
}Running this code produces the following output (image omitted).
The program terminates normally because the daemon thread does not keep the JVM alive after the main thread finishes.
Key Differences
After the main thread ends, user threads continue to run, keeping the JVM alive.
If there are no user threads—only daemon threads—the JVM exits and all daemon threads are terminated.
When all user threads have finished, the program’s business work is done, so the system can shut down; the JVM automatically exits when only daemon threads remain.
Typical Use Cases for Daemon Threads
Daemon threads are suitable for background services that support other user threads, such as heartbeat monitoring, event listening, and the most famous example in Java: garbage collection.
Summary
Java threads are divided into user threads and daemon threads.
When all user threads finish, the JVM exits regardless of the state of daemon threads.
Use Thread.setDaemon() to designate a thread as daemon.
The setDaemon() call must occur before Thread.start() ; calling it later throws an exception and has no effect.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.