Daemon Thread in Java with Examples

In this Java multithreading tutorial, we will discuss what is daemon thread in Java? How to check whether a thread is a daemon or not? How to create a daemon thread in Java? What is the purpose of the daemon thread in Java and many other things?

Java allows to create 2 types of threads they are:-
1) Non-daemon threads
2) Daemon threads

Non-daemon thread:- A thread that executes the main logic of the project is called a non-daemon thread. Example of non-daemon thread:- main thread. In Java, the main thread is responsible for executing the main method, and the execution of a Java program always starts from the main method.

Daemon thread:-  A thread that is running in the background based on a schedule to provide services to non-daemon threads is called daemon thread. Daemon threads are also called service threads. Examples of daemon thread in Java are:- garbage collector, attach listener, signal dispatcher, and e.t.c.

For better understanding purposes we can consider a program as a movie, where the main lead role is played by the non-daemon thread and all necessary support is provided by the daemon thread like the producer, director, makeup artist, music director, and e.t.c. As end-user, we mainly see only the main lead role player on the screen but in the background, there were many people to make the movie.

Daemon Process in Java

What is the main purpose of the daemon thread? The main objective of the daemon thread is to provide support for non-daemon threads (like the main thread). For example:- If the main thread runs with low memory then JVM runs a garbage collector to destroy useless objects. So that the number of bytes of free memory will be improved. With this free memory, the main thread can continue its execution.

Usually, daemon threads are having low priority but based on our requirement daemon threads can run with high priority also.

Default Nature of a Thread

What is the default nature of thread, is it daemon or non-daemon? When we create a new thread then by default it will be a daemon or non-daemon thread?

By default, the main thread is always a non-daemon thread. When we create a new thread then the default nature will be inherited from the parent thread to the child thread. If the parent thread is a daemon thread then the child thread also will be a daemon thread, similarly, if the parent thread is a non-daemon thread then the child thread also will be a non-daemon thread.

Thread class in Java has the below method to check whether the thread is daemon or non-daemon thread:-

public final boolean isDaemon()

Return value of the isDaemon() method,

  • true:- If thread is daemon.
  • false:- If thread is non-daemon.

Java program to check whether the thread is daemon or not?

public class Test extends Thread {
   public static void main(String[] args) {
      System.out.println("Is main thread daemon: " 
         + Thread.currentThread().isDaemon());

      // child thread
      Test t1 = new Test();
      System.out.println("Is child thread daemon: " 
         + t1.isDaemon());
   }
}

Output:-

Is main thread daemon: false
Is child thread daemon: false

Since by default the main thread is non-daemon, therefore, the child thread created by the main thread is also a non-daemon thread.

How to Create a Daemon Thread in Java?

The thread class has the below method to create a user-defined thread as daemon thread:-

public final void setDaemon(boolean flag)

Here if the passed parameter is:-

  • true:- Then thread is created as daemon.
  • false:- Then thread is created as non-daemon.

Program to create a daemon thread in Java,

public class Test extends Thread {
   public static void main(String[] args) {
      // child thread
      Test t1 = new Test();
      System.out.println("Is child thread daemon: " 
                        + t1.isDaemon());

      // change daemon state
      t1.setDaemon(true);
      System.out.println("Is child thread daemon: " 
                        + t1.isDaemon());
   }
}

Output:-

Is child thread daemon: false
Is child thread daemon: true

Note:- Changing the daemon nature of a thread is only applicable before starting the thread. Once a thread is started using start() then we can’t call the setDaemon() method. After starting a thread if we are trying to change daemon nature then we will get a run-time exception: java.lang.IllegalThreadStateException. It is an unchecked exception.

public class Test extends Thread {
   public static void main(String[] args) {

      // create thread
      Test t = new Test();
      // start the thread
      t.start();

      // change daemon nature of thread
      t.setDaemon(true);
   }
}

Output:-

Exception in thread “main” java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.setDaemon(Thread.java:1400)
at Test.main(Test.java:9)

Since thread is already started, therefore on calling setDaemon() we are getting java.lang.IllegalThreadStateException. 

Is it possible to change the daemon nature of the main thread? No. There is no way to change the daemon nature of the main thread because the main thread is already started by the JVM at the very beginning. Therefore it is impossible to change the daemon nature of the main thread.

public class Test {
   public static void main(String[] args) {
      System.out.println("Main thread already started by JVM");
      Thread.currentThread().setDaemon(true);
      Thread.currentThread().setDaemon(false);
   }
}

Output:-

Main thread already started by JVM
Exception in thread “main” java.lang.IllegalThreadStateException

When Daemon thread Completed?

The main purpose of the daemon thread in Java is to provide services to non-daemon threads. If there is no non-daemon thread left then daemon threads also will be terminated.

Whenever the last non-daemon thread in the program reaches into the TERMINATED state (either due to execution completed, or interrupted, or any other reason) then the daemon thread also will be terminated in the middle.

public class Test {
   public static void main(String[] args) {
      MyThread t1 = new MyThread();
      t1.setDaemon(true);
      t1.start();
      System.out.println("End of Main thread");
   }
}

class MyThread extends Thread {
   public void run(){
      for(int i=0; i<5; i++){
         System.out.println("Child Thread: " + i);
         try{
            Thread.sleep(1000);
         } catch (InterruptedException ie) {}
      }
   }
}

Currently, the main thread is non-daemon and the child thread is daemon. Whenever the main thread terminates then the child thread also will be terminated. The child thread will be executed either 0 or 1 times.

Expected Output-1:-

End of Main thread.

Expected Output-2:-

End of Main thread.
Child Thread.

Expected Output-3:-

Child Thread.
End of Main thread.

If we comment the line t1.setDaemon(true); then both threads will be non-daemon threads, and they will run independently from each other. Hence both threads will be executed until their completion.

Use of Daemon Thread in Java

Daemon threads are useful for background supporting tasks such as garbage collection, releasing the memory of unused objects, and removing unwanted entries from the cache. Most of the JVM threads are daemon threads.

Example:- Garbage collector is a daemon thread in Java. Since the garbage collector provides service – destroying unreferenced objects – it is created as a daemon thread. It is a low priority thread so we cannot guarantee its execution, and also it is terminated if all non-daemon threads execution is completed.

Conclusion on Daemon Thread in Java

  1. A thread that is running in the background based on schedule to provide services to non-daemon threads is called daemon thread.
  2. We can check the daemon nature of the thread by using the isDaemon() method of the Thread class.
  3. We can change the daemon nature of the thread by using the setDaemon() method of Thread class. It must be called before starting of the thread else we will get java.lang.IllegalThreadStateException.
  4. Child thread daemon nature is inherited from parent thread.
  5. The default nature of the main thread is non-daemon.
  6. Since daemon threads are service threads, therefore its execution is terminated automatically when all non-daemon threads execution is completed.

Q) Daemon thread runs in

a) Background
b) Foreground
c) Both
d) None of these

Answer:- a) Background; Daemon threads always run in the background to provide support for non-daemon threads. See more:- MCQ on Java multithreading.

Also see:- Interview questions on Daemon thread and Java multithreading, Deadlock in Java.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *