1. Introduction to Threads in Java
In Java, threads are used to perform tasks concurrently. There are two types of threads:
- User Threads: These are the threads that perform application tasks. The JVM will continue running as long as at least one user thread is alive.
- Daemon Threads: These are background service threads that support user threads. The JVM will exit as soon as all user threads are finished, regardless of whether daemon threads are still running.
2. What is a Daemon Thread?
A Daemon Thread in Java is a thread that runs in the background and does not prevent the JVM from exiting.
Examples of daemon threads:
- Garbage Collector
- Finalizer
- Attach Listener
- Timer thread (sometimes)
These threads are typically used to provide services in the background for other threads.
3. Characteristics of Daemon Threads
Property | Description |
Lifecycle | Dies automatically when all user threads die |
Priority | Usually low |
JVM Exit | JVM exits even if daemon threads are running |
Use case | Performing background tasks (like logging, cleanup, etc.) |
Thread type | Must be explicitly marked as daemon before starting the thread |
4. How to Create a Daemon Thread in Java
To make a thread a daemon, use the method:
thread.setDaemon(true);
Important: You must call setDaemon(true)
before calling start()
on the thread.
5. Example: Creating a Simple Daemon Thread
public class DaemonThreadExample { public static void main(String[] args) { Thread daemonThread = new Thread(() -> { while (true) { System.out.println("Daemon thread is running..."); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("Daemon thread interrupted"); } } }); daemonThread.setDaemon(true); // Set before start daemonThread.start(); System.out.println("Main thread ends here."); } }
Output:
You may only see the daemon thread run a few times before the JVM exits because the main thread (user thread) ends quickly.
6. Daemon vs User Thread – Comparison
Feature | User Thread | Daemon Thread |
JVM Lifecycle | JVM waits for them | JVM does not wait |
Purpose | Main application logic | Background service tasks |
Shutdown behavior | JVM waits till it’s done | JVM terminates it abruptly |
Default daemon? | No | No |
7. Inheritance of Daemon Property
If thread A creates thread B:
B is a daemon if and only if A is a daemon at the time of B’s creation unless explicitly changed.
public class InheritDaemon { public static void main(String[] args) { Thread parent = new Thread(() -> { Thread child = new Thread(() -> { System.out.println("Child daemon: " + Thread.currentThread().isDaemon()); }); child.start(); }); parent.setDaemon(true); parent.start(); } }
Output:
Child daemon: true
8. When NOT to Use Daemon Threads
Avoid daemon threads when:
- The thread must complete its task before shutdown (e.g., writing to a file, network request).
- The task has side effects that should not be lost.
- It is managing resources that need proper cleanup.
9. Daemon Thread Termination Behavior
If the JVM shuts down while daemon threads are running:
- Daemon threads are abruptly terminated.
- Finally blocks, shutdown hooks, and try-with-resources may not run.
- You can use Shutdown Hooks to handle proper cleanup.
Runtime.getRuntime().addShutdownHook(new Thread(() -> { System.out.println("Shutdown Hook called!"); }));
10. Check if Thread is Daemon
You can check daemon status using:
System.out.println(Thread.currentThread().isDaemon());