Clean β’ Professional
A daemon thread in Java is a lowβpriority background service thread that runs behind the scenes to support user (nonβdaemon) threads. Its job is to perform supporting or auxiliary tasks, not core application logic.
Most important rule:
When all user threads finish execution, the JVM automatically terminates all daemon threads, even if they are still running.
In simple words:
Daemon threads exist only to serve user threads. Once user threads end, daemon threads die immediately.
Daemon threads help keep applications efficient by handling background work such as:
They ensure these tasks do not block JVM shutdown.
A user thread is a normal thread that performs the main application logic. These threads are critical for program execution.
The JVM waits for all user threads to complete before shutting down.
Examples:
| Basis of Comparison | User Thread | Daemon Thread |
|---|---|---|
| Definition | Normal thread that performs core application work | Background service thread that supports user threads |
| Main Purpose | Executes business logic | Performs helper or supporting tasks |
| JVM Behavior | JVM waits for completion | JVM does NOT wait |
| JVM Shutdown | JVM stays alive until finished | JVM exits when only daemon threads remain |
| Importance of Task | Critical | Nonβcritical |
| Lifecycle Control | Controlled by program | Controlled by JVM |
| Default Nature | Default thread type | Must be explicitly set |
| Creation Rule | Created normally | Marked using setDaemon(true) before start |
| Inheritance Rule | Child thread becomes user thread | Child thread becomes daemon thread |
| Data Safety | Safe for file & DB operations | Unsafe for critical operations |
| Cleanup Guarantee | Cleanup is guaranteed | Cleanup not guaranteed |
| Priority | Normal or high | Usually low |
| Infinite Loop Impact | Can block JVM | JVM can terminate it |
| Examples | Main, worker threads | Garbage Collector, logging threads |
| RealβWorld Analogy | Employees doing main work | Support staff like cleaners |
A thread must be marked as daemon before it is started.
Thread t = new Thread(() -> {
while (true) {
System.out.println("Daemon thread running...");
}
});
t.setDaemon(true); // must be before start()
t.start();
π Calling setDaemon(true) after start() throws IllegalThreadStateException.
class Test extends Thread {
public void run() {
System.out.println(Thread.currentThread().isDaemon());
}
public static void main(String[] args) {
Test t = new Test();
t.setDaemon(true);
t.start();
}
}
Output:
true
Daemon threads are service providers, not main workers.
JVM logic:
This prevents background threads from keeping applications alive forever.
Daemon threads can be terminated abruptly by the JVM. As a result:
finally blocks may not executeβ Therefore, daemon threads should never be used for critical logic.

class Cleanup extends Thread {
public void run() {
while (true) {
System.out.println("Cleaning temporary files...");
try {
Thread.sleep(2000);
} catch (Exception e) {}
}
}
public static void main(String[] args) {
Cleanup t = new Cleanup();
t.setDaemon(true);
t.start();
System.out.println("Main work done");
}
}