Lesson 46
Daemon Thread
A daemon thread runs in the background and terminates when all user threads finish.
Java Track
Save progress as you learn.
61 lessons
Lesson content
A daemon thread runs in the background and stops automatically when all user threads finish.
Definition List
- Daemon Thread: A background thread that does not prevent JVM shutdown.
- User Thread: A normal thread performing primary application work.
- JVM: The runtime environment that executes Java programs.
- Background Task: A task running behind the scenes without user interaction.
How Daemon Threads Work
Main Thread Running
↓
Daemon Thread Running
↓
Main Thread Ends
↓
JVM Shuts Down
↓
Daemon Thread Terminates AutomaticallySyntax
// Mark as daemon (before start)
thread.setDaemon(true);
// Check if daemon
thread.isDaemon();Example: Java Daemon Thread
package com.java.Multi_threading;
public class DaemonThreadExample {
public static void main(String[] args) {
Thread daemonThread = new Thread(new DaemonTask());
daemonThread.setDaemon(true);
daemonThread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread exiting. Daemon thread will be terminated.");
}
}
class DaemonTask implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("Daemon thread is running...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}Output
Daemon thread is running...
Daemon thread is running...
Daemon thread is running...
Main thread exiting. Daemon thread will be terminated.Important Rule: Set Daemon Before Starting
// Correct
thread.setDaemon(true);
thread.start();
// Incorrect — throws IllegalThreadStateException
thread.start();
thread.setDaemon(true);Daemon Thread vs User Thread
- Daemon — runs background/supporting tasks.
- User — runs primary application logic.
- Daemon — does not prevent JVM shutdown.
- User — JVM waits until all user threads finish.
- Daemon — lifetime depends on user threads.
- User — lifetime is independent.
Best Practices
- Always call setDaemon(true) before start().
- Use for monitoring, logging and maintenance tasks.
- Never use for critical data operations.
- Handle exceptions gracefully inside daemon threads.
FAQ
1. What is a daemon thread in Java?
A background thread that supports application tasks and terminates automatically when all user threads finish.
2. How do you create a daemon thread?
Call setDaemon(true) before invoking start() on the thread.
3. What happens to daemon threads on JVM shutdown?
The JVM automatically terminates all daemon threads when no user threads remain active.
Where Java is used
Java in real-world development
Daemon Thread
A background thread that terminates automatically when the JVM shuts down.
setDaemon(true)
Marks a thread as daemon. Must be called before start().
isDaemon()
Returns true if the thread is a daemon thread.
JVM Shutdown
JVM exits when all user threads finish; daemon threads are stopped automatically.
User Thread
A normal thread. JVM keeps running until all user threads complete.
Use Cases
Garbage collection, background monitoring, logging, cache cleanup.