본문 바로가기
  • think normal
새로워지기/서른의 생활코딩

ex12) java_thread

by 청춘만화 2012. 3. 4.
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");
       }
    }

}

'새로워지기 > 서른의 생활코딩' 카테고리의 다른 글

ex1) java.awt.*  (0) 2012.03.04
java _2012.02.28  (0) 2012.03.04
ex11) java_thread  (0) 2012.03.04
ex10) java_thread  (0) 2012.03.04
ex9) java_thread  (0) 2012.03.04

댓글