Thursday, September 12, 2013

Thread : 03 Basic contd. [Daemon Thread]


Thread Basic contd. [Daemon Thread]

2 types of threads.

1. Daemon Thread
* The Daemon Thread is a service provider thread.
* It provides services to the user thread.
* Its life depends on the user threads. I.e when all the user threads dies, JVM terminates the daemon thread automatically.

2. User Thread

Methods of a Daemon Thread
* setDaemon(boolean true);
* isDaemon();

/**
* @author sanjeeva
* Daemon Thread
* - The Daemon Thread is a service provider thread.
* - It provides services to the user thread.
* - Its life depends on the user threads. I.e when all the user threads dies, JVM terminates the daemon thread automatically.
*/
public class DaemonThread extends Thread{

        public void run(){
               System.out.println("Name : " + Thread.currentThread().getName() + " Daemon : " + Thread.currentThread().isDaemon());
        }

       /**
       * @param args
       */
      public static void main(String[] args) {

            DaemonThread dt1 = new DaemonThread();
            DaemonThread dt2 = new DaemonThread();

            dt1.setDaemon(true);
            dt1.start();
            dt2.start();
      }
}


Name : Thread-0 Daemon : true
Name : Thread-1 Daemon : false