Daemon Thread in Java

 

Basically in Java there are 2 types of threads and they are Daemon Thread and non-Daemon Thread (User threads). Daemon threads are nothing but service provider for other threads to perform their task under same process like Garbage collection etc., Any Java thread can be a daemon thread and daemon threads life depends on user threads. It will be alive until all other threads are running and JVM will terminate daemon thread automatically once all other threads died. Some of the points to be remembered about daemon thread are 

  • Daemon threads are low priority threads. 
  • Daemon threads life depends on all other user threads (non-daemon threads) in same process.
  • Finally it provides only service to other user threads and nothing more than that.  
  • To specify the thread as daemon thread, just call the setDaemon method with the argument "true"  like  ( Thread.setDaemon(true) ).
  • Setting daemon should be done before starting the Thread, else it will throw IllegalThreadStateException exception. 
  • JVM will automatically exit the process once all user threads are completed and it won't wait for any Daemon thread to complete, no matter how many Daemon threads are running. 

Lets see small example with creating Daemon Thread and user threads under same process and lets see how its work.


public class DaemonThreadTest extends Thread{
 
 String daemonFlag = "";
 
 public DaemonThreadTest() { }
 
 public DaemonThreadTest(String daemonFlag) {
  this.daemonFlag = daemonFlag;
 }
 
 public void run() {
  if(daemonFlag.equals("daemon")){
   while(true){    
    try {
     System.out.println("Daemon Thread Running !!!");
     Thread.sleep(500);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }else{
   for(int i=0;i<5;i++){
    try {
     System.out.println("User Thread Running !");
     Thread.sleep(500);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }
 
 public static void main(String args[]){
    
  DaemonThreadTest daemonThread = new DaemonThreadTest("daemon");
  DaemonThreadTest userThread1 = new DaemonThreadTest();
  DaemonThreadTest userThread2 = new DaemonThreadTest();
  
  // Setting Daemon thread;
  daemonThread.setDaemon(true);
  
  daemonThread.start();
  userThread1.start();
  userThread2.start();
 }
}


OUTPUT:


Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!

In above sample program we can see Daemon Thread we have set in infinite loop and user threads under looping for 5 times. One all other user thread complete Daemon Thread has be terminated automatically by JVM and process get completed. No more Daemon Thread is running. 

No comments:
Write comments