A daemon thread. : Daemon Thread « Thread « Java Tutorial






class MyDaemon implements Runnable {
  Thread thrd;
  MyDaemon() {
    thrd = new Thread(this);
    thrd.setDaemon(true);
    thrd.start();
  }
  public boolean isDaemon(){
    return thrd.isDaemon();
  }
  public void run() {
    try {
      while(true) {
        System.out.print(".");
        Thread.sleep(100);
      }
    }
    catch(Exception exc) {
      System.out.println("MyDaemon interrupted.");
    }
  }
}

public class Main {
  public static void main(String args[]) throws Exception{
    MyDaemon dt = new MyDaemon();
    if(dt.isDaemon())
      System.out.println("dt is a daemon thread.");

    Thread.sleep(10000);
    System.out.println("\nMain thread ending.");
  }
}








10.7.Daemon Thread
10.7.1.Daemon and User Threads
10.7.2.A daemon thread.
10.7.3.An application exits when there are no non-daemon threads running.
10.7.4.A live daemon thread does not prevent an application from exiting.
10.7.5.A thread must be marked as a daemon thread before it is started.