Daemon Thread in Java

the main objective of the daemon thread is to provide support for the non daemon Thread (main Thread).

For example – if the main Thread runs with low memory then JVM will run a garbage collector to destroy useless objects so that number of bytes in the free memory will be improved. now with this free memory main Thread can continue its execution

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

isDaemon() method in Java

We can check daemon nature of a Thread by using isDaemon() method of Thread class.

public final boolean isDaemon()

setDaemon() method in Java

We can change Daemon nature of a Thread by using setDaemon() method

public final void setDaemon(     boolean on )

But Changes in daemon nature is only possible if we change before starting of Thread. If thread already started and we are trying to change daemon nature then we willl get Run Time Exception ” Exception in thread “main” java.lang.IllegalThreadStateException”

public class DaemonThread {
    public static void main(String[] args) {
        Thread t  = new Thread();
        t.start();
        t.setDaemon(true);
    }
}

Daemon Thread

What is the Default Nature of a Thread

By Default main Thread is always a non-daemon Thread and all remaining Threads’ daemon nature will be inherited from Parent to Child i,e. if the parent Thread is Daemon then the Child thread is also a Daemon Thread and if the parent Thread non-Daemon then Child Thread is also a non-Daemon Thread.

It is impossible to change the Daemon nature of the main Thread because it’s already started by JVM at the beginning.

public class DaemonThread {
    public static void main(String[] args) {
        Thread t  = new Thread();
        System.out.println(t.isDaemon());
        t.setDaemon(true);
        System.out.println(t.isDaemon());
    }
}
daemon Thread in Java

Whenever the last non-Daemon Thread is terminated automatically all daemon Threads will be terminated irrespective of their position.

public class DaemonThread {
    public static void main(String[] args) {
        ChildThread childThread = new ChildThread();
        childThread.setDaemon(true);
        childThread.start();
        System.out.println("Main Thread Ended Here");
    }
}

class ChildThread extends Thread{
    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println("Child Thread");
            try{
                Thread.sleep(2000);
            } catch (InterruptedException e){

            }
        }
    }
}

Daemon Thread and non daemon Thread

In the Above Java program if we comment on the statement childThread.setDaemon(true); then Main and Child Thread both are non-daemon and hence both Threads will keep exception on until completion of their task. In the opposite scenario if we did not comment statement childThread.setDaemon(true); Then the main Thread is non – Daemon and the child thread is Daemon hence whenever the main Thread is terminated, the Child thread is also terminated.

Similar Java Tutorials