Learn Java Basics Concepts

Java Beginners Guide: What is Daemon Thread nature with example program in multithreading core java

Daemon Thread Nature:

The threads which are executing in the backgrounds are called Daemon threads.
Example: Garbage collector, Signal dispatcher, Attach listener.

The main objective of daemon thread is to provide support for non daemon thread (main thread)

For example, if main thread runs with low memory then jvm runs garbage collector to destroy useless objects. So that no of bytes of free memory will be improved with this free memory main thread can continue its execution.

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

We can check daemon nature of thread by using isDaemon() method of thread class.
Public Boolean isDaemon()

We can change daemon nature of thread by using set daemon method.
Public void setDaemon(Boolean b)

But changing daemon nature is possible before starting of thread only. After starting a thread if trying to change the daemon nature then we will get runtime exception saying illegalthreadstateException.

By default main thread is always un demand for all remaining threads daemon nature will be inherited from parent to child if parent thread is daemon then automatically child thread is also daemon and if parent thread is non daemon then automatically child thread also non daemon.

Daemon Thread Example:


package Multi;
class ThreadA extends Thread{


}
class DaemonThreadExample {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().isDaemon());
       //Thread.currentThread().setDaemon(true);
        ThreadA t=new ThreadA();
        System.out.println(t.isDaemon());
        t.setDaemon(true);
        System.out.println(t.isDaemon());
    }
}

output:

false
false
true

Note: 

it is impossible to change daemon nature of main thread because it is already started by jvm at beginning. 
Examples:

Whenever last non-daemon thread terminates automatically all daemon threads will be terminated irrespective of their position.
  

Java Daemon thread Program


package Multithreading;
class MyThread extends Thread{

public void run(){
    for(int i=0;i<=10;i++){
    
        System.out.println("child thread");
    }
try{

Thread.sleep(2000);
}catch(InterruptedException e){}


}

}
       

public class DaemonThreadDemo {
    public static void main(String[] args) {
        MyThread t=new MyThread();
        t.setDaemon(true);//--line 1
        t.start();
        System.out.println("end of main thread");
    }
}
    


  If we are commenting line 1 both main and child threads are non-daemon and hence both threads will be executed until their completion if we are not commenting line1 then main thread is non-daemon and child thread is daemon hence whenever main thread terminate automatically child thread terminated in this case output are:


1. Possibility output

End of main thread
Child thread

2. Possibility output

End of main thread
--------------------

3. Possibility output

Child thread
End of main thread


Tag : Code Java
0 Komentar untuk "Java Beginners Guide: What is Daemon Thread nature with example program in multithreading core java"

Featured Content

Back To Top