Open In App

Java Daemon Thread

Last Updated : 04 Oct, 2025
Suggest changes
Share
Like Article
Like
Report

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

Java
thread.setDaemon(true); 

Use Cases

  1. Garbage Collection: The Garbage Collector (GC) in Java runs as a daemon thread.
  2. Background Monitoring: Daemon threads can monitor the state of application components, resources, or connections.
  3. Logging and Auditing Services: Daemon threads can be used to log background activities continuously.
  4. Cleanup Operations: Daemon threads may periodically clear temporary files, release unused resources, or perform cache cleanup.
  5. 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

Java
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

Java
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

BasisUser ThreadDaemon Thread
PurposeExecutes main application tasksPerforms background services
LifecycleKeeps JVM alive until finishedTerminates when all user threads finish
PriorityUsually higherUsually lower
JVM ExitJVM waits for completionJVM exits even if running
ExamplesMain thread, worker threadsGarbage collector, background monitors

Explore