청춘만화 2012. 3. 4. 11:02
public class DaemonThread {
     static public void main(String s[]) {
     MyThread6 thr_a = new MyThread6();
     MyThread6 thr_b = new MyThread6();

     System.out.println("Starting the threads...");
     thr_a.start();
      thr_b.setDaemon(true);
     thr_b.start();

      try {
      Thread.sleep(2000);
       } catch (InterruptedException e) {}

     System.out.println("Stopping the normal thread...");
     thr_a.stop(true);
     }
}


 class MyThread6 extends Thread {
     protected boolean stop;

     public void stop(boolean b) {
         stop = b;
     }

    public void run() {
       while (!stop) {
              try { 
                 sleep(500); 
              } catch (InterruptedException e) {}
 
           if (isDaemon())
           System.out.println(getName() + ": daemon thread");
           else
           System.out.println(getName() + ": regular thread");
       }
    }

}