Daemon Thread in Java

Daemon thread is a low priority thread (in context of JVM) that runs in background to perform tasks such as garbage collection (gc) etc., they do not prevent the JVM from exiting (even if the daemon thread itself is running) when all the user threads (non-daemon threads) finish their execution. JVM terminates itself when all user threads (non-daemon threads) finish their execution, JVM does not care whether Daemon thread is running or not, if JVM finds running daemon thread (upon completion of user threads), it terminates the thread and after that shutdown itself.

Properties of Daemon threads:
  1. A newly created thread inherits the daemon status of its parent. That’s the reason all threads created inside main method (child threads of main thread) are non-daemon by default, because main thread is non-daemon. However you can make a user thread to Daemon by using setDaemon() method of thread class.

  2. Just a quick note on main thread: When the JVM starts, it creates a thread called “Main”. Your program will run on this thread, unless you create additional threads yourself. The first thing the “Main” thread does is to look for your static void main (String args[]) method and invoke it. That is the entry-point to your program. If you create additional threads in the main method those threads would be the child threads of main thread.

  3. Methods of Thread class that are related to Daemon threads:

  4. public void setDaemon(boolean status): This method is used for making a user thread to Daemon thread or vice versa. For example if I have a user thread t then t.setDaemon(true) would make it Daemon thread. On the other hand if I have a Daemon thread td then by calling td.setDaemon(false) would make it normal thread(user thread/non-daemon thread).

  5. public boolean isDaemon(): This method is used for checking the status of a thread. It returns true if the thread is Daemon else it returns false.

  6. setDaemon() method can only be called before starting the thread. This method would throw IllegalThreadStateException if you call this method after Thread.start() method. (refer the example)

Points to remember for Daemon Thread in Java
  • It provides services to user threads for background supporting tasks. It has no role in life than to serve user threads.

  • Its life depends on user threads.

  • It is a low priority thread.

Why JVM terminates the daemon thread if there is no user thread?

The sole purpose of the daemon thread is that it provides services to user thread for background supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM terminates the daemon thread if there is no user thread.

Methods for Java Daemon thread by Thread class

The java.lang.Thread class provides two methods for java daemon thread.

No.MethodDescription
1)public void setDaemon(boolean status)is used to mark the current thread as daemon thread or user thread.
2)public boolean isDaemon()is used to check that current is daemon.
Simple example of Daemon thread in java
public class Test extends Thread{ public void run(){ if(Thread.currentThread().isDaemon()) {//checking for daemon thread System.out.println("daemon thread work"); } else{ System.out.println("user thread work"); } } public static void main(String[] args){ Test t1=new Test();//creating thread Test t2=new Test(); Test t3=new Test(); t1.setDaemon(true);//now t1 is daemon thread t1.start();//starting threads t2.start(); t3.start(); } }


Output:
daemon thread work user thread work user thread work
Daemon thread examples

This example is to demonstrate the usage of setDaemon() and isDaemon() method.

public class Test extends Thread { public void run(){ // Checking whether the thread is Daemon or not if(Thread.currentThread().isDaemon()){ System.out.println("Daemon thread executing"); } else{ System.out.println("user(normal) thread executing"); } } public static void main(String[] args) { /* Creating two threads: by default they are * user threads (non-daemon threads) */ Test t1=new Test(); Test t2=new Test(); //Making user thread t1 to Daemon t1.setDaemon(true); //starting both the threads t1.start(); t2.start(); } }


Output:
Daemon thread executing user(normal) thread executing
Daemon thread examples

If you call the setDaemon() method after starting the thread (start() method), it would throw IllegalThreadStateException. This clearly means that you can call setDaemon() method only before starting a thread.

public class Test extends Thread { public void run(){ System.out.println("Thread is running"); } public static void main(String[] args){ Test t1=new Test(); t1.start(); // It will throw IllegalThreadStateException t1.setDaemon(true); } }


Output:
Thread is running Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.setDaemon(Thread.java:1232) at Test.main(Test.java:11)
File: MyThread.java
class Test extends Thread{ public void run(){ System.out.println("Name: "+Thread.currentThread().getName()); System.out.println("Daemon: "+Thread.currentThread().isDaemon()); } public static void main(String[] args){ Test t1=new Test(); Test t2=new Test(); t1.start(); t1.setDaemon(true);//will throw exception here t2.start(); } }


Output:
Name: Thread-0 Daemon: false Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.setDaemon(Thread.java:1232) at Test.main(Test.java:11)
Difference between Daemon threads and non-Daemon thread (user thread)

The main difference between Daemon thread and user threads is that the JVM does not wait for Daemon thread before exiting while it waits for user threads, it does not exit until unless all the user threads finish their execution.




Instagram