Daemon Threads are the threads which are executing in the background. They are normally low priority threads and the main objective of daemon threads is to provide support for non-daemon threads. The best example of a daemon thread is Java Garbage Collector.
For example, if main thread runs with low memory, then JVM runs garbage collector to destroy useless objects so that free memory will be improved. With this free memory, main thread continues its execution.
Thus, garbage collector in Java can be considered a daemon thread which provides support for the main thread.
Usually, daemon thread runs with low priority. But based on the requirement, daemon threads can run with high priority also.
We can check daemon nature of a thread by using public boolean isDaemon() method of thread class.
We can also change daemon nature of a thread by using public void setDaemon(boolean b) method. But changing daemon nature is possible only before starting a thread.
It is impossible to change daemon nature of the main thread. Because it is already started by JVM at the beginning and we cannot change daemon nature of a running thread.
If we try to change daemon nature of a thread after starting the thread, we will get Runtime Exception – Illegal Thread State Exception.
class ChildThread extends Thread { public void run() {} } public class Demo { public static void main(String[] args) { System.out.println("Main Thread is a Daemon Thread -> " + Thread.currentThread().isDaemon() ); Thread.currentThread().setDaemon(true); } }
Output:
Main Thread is a Daemon Thread -> false Exception in thread "main" java.lang.IllegalThreadStateException at java.base/java.lang.Thread.setDaemon(Unknown Source) at Demo.main(Demo.java:11)
By default, the main thread is always non-daemon in nature. For all remaining threads, daemon nature will be inherited from parent to child.
Example of Daemon threads
class ChildThread extends Thread { public void run() { } } public class Demo { public static void main(String[] args) { System.out.println("Main Thread is a Daemon Thread -> " + Thread.currentThread().isDaemon() ); ChildThread child_thread = new ChildThread(); System.out.println("Child Thread is a Daemon Thread -> " + child_thread.isDaemon() ); child_thread.setDaemon(true); System.out.println("Child Thread is a Daemon Thread -> " + child_thread.isDaemon() ); } }
In this example, there are two threads: main thread and child thread.
It is a fact that the main thread is a non-daemon thread as JVM starts it initially.
Since main thread is creating child thread, the child thread is also non-daemon in nature. We then set child thread to be a daemon thread.
Look at the output of above code for better understanding.
Output:
Main Thread is a Daemon Thread -> false Child Thread is a Daemon Thread -> false Child Thread is a Daemon Thread -> true
Whenever last non-daemon thread terminates, automatically all daemon thread will be terminated irrespective of their position.
Example of Termination of a Daemon thread
class ChildThread extends Thread { public void run() { for(int i=0;i<10;i++) { try { Thread.sleep(2000); } catch(InterruptedException exp) { System.out.println("Child Thread got interrupted"); } System.out.println("Child Thread is executing"); } } } public class Demo { public static void main(String[] args) throws InterruptedException { System.out.println("Main Thread has started its execution"); ChildThread child_thread = new ChildThread(); child_thread.setDaemon(true); System.out.println("Child Thread is a Daemon Thread -> " + child_thread.isDaemon() ); child_thread.start(); Thread.sleep(11000); System.out.println("Main Thread has finished its execution"); } }
In the above code, we first make child thread a daemon thread.
Both the main and child thread run concurrently. Main Thread sleeps for 11 seconds. Meanwhile, child thread prints one statement 5 times every two seconds.
When the main thread finishes its execution, there are no non-daemon threads left.
Hence, our child thread will be terminated as it is a daemon thread. So, instead of printing a statement 10 times in total 20 seconds, it will just print it 5 times during the time span of 11 seconds.
Look at the output below for better understanding.
Output:
Main Thread has started its execution Child Thread is a Daemon Thread -> true Child Thread is executing Child Thread is executing Child Thread is executing Child Thread is executing Child Thread is executing Main Thread has finished its execution
However, if we do not make child thread to be a daemon thread and allow it to become non-daemon thread by the inheritance from main thread, then both the threads will complete their entire execution successfully.
In that case, the output will be as following:
Main Thread has started its execution Child Thread is a Daemon Thread -> false Child Thread is executing Child Thread is executing Child Thread is executing Child Thread is executing Child Thread is executing Main Thread has finished its execution Child Thread is executing Child Thread is executing Child Thread is executing Child Thread is executing Child Thread is executing
This is all about daemon threads in Java. Hope you find this tutorial helpful.