A daemon thread is a low-priority background thread that does not prevent the JVM from exiting when the program finishes execution
- Daemon threads run in the background to support user threads.
- The JVM exits automatically when all user (non-daemon) threads complete.
- They are created using the same Thread class but are marked as daemon using the setDaemon(true) method.
- The setDaemon(true) method must be called before the thread starts.
- Common examples include Garbage Collector and Finalizer Thread.
Syntax
thread.setDaemon(true);
Use Cases
- Garbage Collection: The Garbage Collector (GC) in Java runs as a daemon thread.
- Background Monitoring: Daemon threads can monitor the state of application components, resources, or connections.
- Logging and Auditing Services: Daemon threads can be used to log background activities continuously.
- Cleanup Operations: Daemon threads may periodically clear temporary files, release unused resources, or perform cache cleanup.
- Scheduler or Timer Tasks: Background schedulers often use daemon threads to trigger tasks at fixed intervals.
Methods Used
- void setDaemon(boolean on): Marks a thread as daemon or user thread. Must be called before start().
- boolean isDaemon(): Checks whether a thread is daemon.
Creating a Daemon Thread
public class DaemonExample extends Thread {
public void run() {
if (Thread.currentThread().isDaemon()) {
System.out.println("Daemon thread running...");
} else {
System.out.println("User thread running...");
}
}
public static void main(String[] args) {
DaemonExample t1 = new DaemonExample();
DaemonExample t2 = new DaemonExample();
t1.setDaemon(true); // must be set before start()
t1.start();
t2.start();
}
}
Output
Daemon thread running... User thread running...
Behavior of Daemon Thread
public class DaemonBehavior extends Thread {
public void run() {
while (true) {
System.out.println("Daemon thread running...");
}
}
public static void main(String[] args) {
DaemonBehavior t = new DaemonBehavior();
t.setDaemon(true);
t.start();
System.out.println("Main (user) thread ends...");
}
}
Output
Main (user) thread ends...
The JVM ends immediately after the main thread finishes, even though the daemon thread is still running.
Important Notes
- A thread inherits the daemon status of the thread that creates it.
- The setDaemon(true) method throws IllegalThreadStateException if called after start().
- Daemon threads should not be used for tasks requiring completion, such as writing to a file or updating a database.
- JVM terminates all daemon threads abruptly without performing cleanup operations.
Difference Between User Thread and Daemon Thread
| Basis | User Thread | Daemon Thread |
|---|---|---|
| Purpose | Executes main application tasks | Performs background services |
| Lifecycle | Keeps JVM alive until finished | Terminates when all user threads finish |
| Priority | Usually higher | Usually lower |
| JVM Exit | JVM waits for completion | JVM exits even if running |
| Examples | Main thread, worker threads | Garbage collector, background monitors |