Cyh的博客

          Email:kissyan4916@163.com
          posts - 26, comments - 19, trackbacks - 0, articles - 220

          線程8--守護線程Daemon

          Posted on 2009-06-02 21:26 啥都寫點 閱讀(279) 評論(0)  編輯  收藏 所屬分類: J2SE
          關鍵技術:
          • Thread的setDaemon實例方法設置線程是否為守護線程,參數為true表示該線程為守護線程。
          • 線程被運行后,setDaemon實例方法無效,即必須在調用start方法之前調用setDaemon方法,才能設置線程為守護線程。
          • 程序中啟動的線程默認為非守護線程,但在守護線程中啟動的線程都是守護線程。
          • 當程序中所有非守護線程都結束時,守護線程無條件地被立即結束。

          package book.thread;

          /**
           * Daemon(守護)線程 
           * Daemon線程區別一般線程之處是: 
           * 只有虛擬機中的用戶線程(非Daimon線程)全部結束,Daemon線程就會立即結束,并且也不會調用finally里的語句。
           * daemon線程所產生的所有線程都是daemon的
           
          */
          public class Daemon {

              
          static class MainThread extends Thread {

                  
          public void run() {
                      System.out.println(
          "MainThread is daemon? " + this.isDaemon());
                      System.out.println(
          "MainThread begin!");
                      
          //啟動子線程
                      Thread sub1 = new SubThread();
                      
          //sub1線程為守護線程
                      sub1.setDaemon(true);
                      sub1.start();
                      
          try {
                          Thread.sleep(
          1000);
                      }
          catch (InterruptedException e) {
                          e.printStackTrace();
                      } 
          finally {
                          System.out.println(
          "MainThread" + " finally");
                      }
                      System.out.println(
          "MainThread end!");
                  }
              }

              
          static class SubThread extends Thread {

                  
          public void run() {
                      System.out.println(
          "SubThread is daemon? " + this.isDaemon());
                      System.out.println(
          "SubThread begin!");
                      
          int i = 0;
                      
          try {
                          
          while (i < 10) {
                              System.out.println(
          "SubThread  " + i++);
                              Thread.sleep(
          200);
                          }
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      } 
          finally {
                          System.out.println(
          "SubThread finally");
                      }
                      System.out.println(
          "SubThread end!");
                  }
              }

              
          public static void main(String[] args) {
                  System.out.println(
          "Main begin!");
                  
          //默認情況下mainThread是普通線程
                  Thread mainThread = new MainThread();
                  
          //啟動mainThread線程
                  mainThread.start();
                  
          try {
                      Thread.sleep(
          500);
                  } 
          catch (InterruptedException e1) {
                      e1.printStackTrace();
                  }
                  System.out.println(
          "Main end!");
              }
          }




                                                                                                                 --    學海無涯
                  

          主站蜘蛛池模板: 余姚市| 抚宁县| 日照市| 临猗县| 卢湾区| 黄浦区| 安图县| 瑞安市| 蒲江县| 兴城市| 常宁市| 勐海县| 临夏县| 玛沁县| 临沭县| 上虞市| 溆浦县| 北碚区| 名山县| 台南县| 绥棱县| 龙口市| 建湖县| 泗阳县| 三门峡市| 荥阳市| 化德县| 娱乐| 福泉市| 喜德县| 邓州市| 平定县| 仁化县| 礼泉县| 郑州市| 随州市| 张掖市| 芜湖市| 上犹县| 开江县| 城固县|