Daemon Threads in Java
Fast recall
Quick revision
Core answer
start(). Do not use daemon threads for work that must flush, save, or complete reliably.Understanding of JVM lifecycle and how threads affect application shutdown behavior.
Setting daemon status after calling start(), which throws IllegalThreadStateException and has no effect.
Give a concrete production example like a health check monitor that polls metrics every 5 seconds but shouldn't block application shutdown.
Interview-ready guide
Interview answer
A daemon thread is a thread that does not prevent JVM shutdown. If every started non-daemon thread has finished, the JVM can exit even though daemon threads are still running. For example, an optional in-memory metrics sampler could be daemon work because losing its final sample is acceptable. A thread writing an audit record should not be daemon work, because shutdown may stop it before the record is stored.
The daemon flag must be set before start(). For most application services I prefer an executor owned by the application and shut it down explicitly, because that gives required tasks a controlled chance to finish.
Daemon Threads in Java
Direct Answer
A daemon thread is a Java thread that does not keep the JVM alive. When no started non-daemon thread remains, the JVM may exit even if daemon threads still have work. Set daemon status before start(). Do not use daemon threads for work that must flush, save, or complete reliably.
Code Example
import java.time.*;
class HealthCheckMonitor implements Runnable {
@Override
public void run() {
while (true) { // an infinite loop is fine for a daemon
System.out.println("[" + LocalTime.now() + "] Health check: System OK");
try {
Thread.sleep(5000); // check every 5 seconds
} catch (InterruptedException e) {
return;
}
}
}
}
class Application {
public static void main(String[] args) throws InterruptedException {
var monitor = new Thread(new HealthCheckMonitor());
monitor.setDaemon(true); // MUST be before start()
monitor.start();
System.out.println("Application started");
Thread.sleep(12000); // run for 12 seconds
System.out.println("Application finishing");
// When main (a user thread) exits, the daemon is killed automatically
}
}
Common Mistakes & Pitfalls
- Setting daemon after
start():setDaemon(true)after the thread starts throwsIllegalThreadStateException. It must be set beforestart(). - Using a daemon for critical work: If the JVM shuts down, the daemon is killed mid-operation, risking corruption or an incomplete transaction.
- Expecting cleanup to run: Daemon threads are terminated abruptly, so
finallyblocks don't execute. Use a JVM shutdown hook for guaranteed cleanup.
Check your understanding
Answer these without looking back at the article.
- Can a daemon thread create a non-daemon thread?
- Does a daemon thread get a finally block executed?
- What happens to a daemon thread if the main thread throws an exception?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.