Allowing an Application with Live Threads to Exit - Java Thread

Java examples for Thread:Thread Operation

Description

Allowing an Application with Live Threads to Exit

Demo Code



public class Main {
  public static void main(String[] args) {
    // Create the thread
    Thread thread = new MyThread();

    // Thread can be set as daemon by the creator
    thread.setDaemon(true);/*from  w  w  w. ja  va  2  s .  c  o  m*/

    // Start the thread
    thread.start();
  }
}

class MyThread extends Thread {
  MyThread() {
    // Thread can be set as a daemon thread in the constructor
    setDaemon(true);
  }

  // This method is called when the thread runs
  public void run() {
    // Determine if this thread is a daemon thread
    boolean isDaemon = isDaemon();
  }
}

Related Tutorials