Wednesday, 30 September 2015

Java Daemon Thread

package com.JavaInterview.Programs;
/**
 * *You can make any java thread as daemon thread. Daemon threads acts like service providers
 *  for other threads running in the same process.Daemon threads will be terminated by the 
 *  JVM when there are none of the other threads running, it includes main thread of execution 
 *  as well.
 * 
 */
public class DaemonThread extends Thread{
    
   public DaemonThread(){
       setDaemon(true);
   }
   public void run(){
       System.out.println("Is this thread Daemon? - "+isDaemon());
   }
   public static void main(String a[]){
       DaemonThread dt = new DaemonThread();
       // you can set daemon constrain here also
       // it is like dt.setDeamon(true)
       dt.start();
   }
}

Output:
Is this thread Daemon? - true


No comments:

Post a Comment

Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...