Wednesday 24 July 2019

Daemon Threads in Multithreading

  • The Threads which are executing in the background are called daemon Threads. Example: Garbage collector, Signal dispatcher, attach listener etc.
  • 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 than JVM runs garbage collector to destroy useless objects. So that number 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 deamon nature of a Thread by using isDaemon() method of Thread class.
    • public boolean isDaemon();
  • We can change daemon nature of a Thread by using setDaemon() method.
    • public void setDaemon(boolean b);
  • But changing daemon nature is possible before starting of a Thread only. After starting the Thread if we are trying to change the daemon nature we will get runtime exception saying IllegalThreadStateException.
  • By default main Thread is always non demon and all remaining Threads daemon nature will be inherited from parent to child i.e if parent thread is daemon then automatically child thread will be daemon. If parent Thread is non daemon then automatically child Thread also non daemon.
NOTE: It is impossible to change daemon nature of main Thread because it is already started by JVM at beginning. 

Example:

class MyThread extends Thread
{
}

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


Output:
false
false
true
  • Whenever last non-daemon Thread terminates automatically all daemon Threads will be terminated irrespective of their position.
Example:

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)
                                           {}
                             }
              }
}

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


Output:
End of main Thread
  • If we are commenting line 1 both main and child Threads are non-daemon. Hence both Threads will be executed until their completion.
  • If we are not commenting line 1 then main Thread is non-daemon and child Thread is daemon. Hence whenever main Thread terminates automatically child Thread will be terminated. In this case output is “End of main Thread” followed by “Child Thread” or sometimes we may get “End of main Thread” or “Child Thread” followed by “End of main Thread”.
Thanks..!!

No comments:

Post a Comment