March 30, 2024

Daemon Thread in Java

Daemon thread in Java is a thread that runs in background to perform some tasks for the program as long as the program is running. Even if daemon threads are running that doesn’t prevent JVM from shutting down.

Features of daemon thread in Java

  1. The Java Virtual Machine exits when the only threads running are all daemon threads. So, the JVM waits only for the non-demon threads in a program to complete.

    That is why daemon threads in Java are suitable for performing general tasks like clearing the in-memory cache which may be essential to a program but not the main part of the program. Even the JVM, when it starts, creates daemon threads for garbage collection and other book keeping tasks and a non-daemon main thread for executing the program. That is also the reason why JVM doesn’t wait for daemon threads to finish as daemon thread in Java is supposed to perform some background tasks for the user threads. If user threads have completed there is no sense in executing the daemon thread.

  2. Whether a thread is daemon or not that property is inherited from the thread that spawned the other thread. Since main thread in Java is non-daemon so any thread spawned in main thread will be non-daemon.
  3. Same way any thread created by a daemon thread is automatically a daemon thread.
  4. If you want to mark a thread as either a daemon thread or a user thread that can be done using setDaemon(boolean on) method of the Thread class in Java. This method can only be called before the thread has been started i.e. before calling thread.start() method. Otherwise IllegalThreadStateException is thrown.
  5. Another method of the Thread class isDaemon() tests if thread is a daemon thread. Returns true is calling thread is a daemon thread; false otherwise.

Daemon thread Java example

In the example code there are two thread classes DaemonThread and AnotherDaemon. Initially when a DaemonThread instance is created it would be a non-daemon thread as it is created in main thread which itself is non-daemon, later it is changed to a daemon thread using setDaemon() method. On the other hand instance of AnotherDaemon would be a daemon thread as it is created in DaemonThread which is now a daemon thread.

class DaemonThread extends Thread{
  public void run() {
    // Creating another thread from a demon thread
    Thread ad = new AnotherDaemon();
    System.out.println("DaemonThread daemon -- " + isDaemon());
    System.out.println("AnotherDaemon daemon -- " + ad.isDaemon());

class AnotherDaemon extends Thread{
  public void run() {
    System.out.println("AnotherDaemon thread run() method");

public class TestThread {
  public static void main(String[] args) {
    Thread dt = new DaemonThread();
    System.out.println("DaemonThread daemon -- " + dt.isDaemon());
    // marking as daemon thread
    try {
      // delay to let the daemon thread execute, 
      // otherwise JVM exits after completing the
      // execution of the main thread.
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
DaemonThread daemon -- false
DaemonThread daemon -- true
AnotherDaemon daemon -- true

That's all for the topic Daemon Thread in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

No comments:

Post a Comment