白話Java - 守護線程

OneCoder(苦逼Coder)原創,轉載請務必註明出處: http://www.coderli.com/archives/daemon-thread-plain-words/ web

關於「白話」:偶然想到的詞,也許有一天能成爲一個系列。目的就是用簡潔,明快的語言來告訴您,我所知道的一切。ide

 
Java中的線程分兩類,用戶線程和守護線程。
  
  
           
  
  
  1. Thread commonThread = new Thread("Common Thread"); 

這樣就是用戶線程。測試

  
  
           
  
  
  1. Thread daemonThread = new Thread("Daemon Thread"); 
  2. daemonThread.setDaemon(true); 

這樣就是守護線程。this

起了「守護」線程這麼動聽的名字,天然要起到「守護」的做用。就比如男人要守護妹子。spa

守護線程的做用,按照網上的說法是: 守護線程則是用來服務用戶線程的,若是沒有其餘用戶線程在運行,那麼就沒有可服務對象,也就沒有理由繼續下去。
 
說白了就是妹子沒了,男人也就自盡了。分狀況寫幾個例子,一跑便知。
 
  • 兩個妹子 - 互不想幹,你掛你的,我掛個人

 

   
   
            
   
   
  1. /** 
  2.      * 測試兩個用戶線程的狀況 
  3.      *  
  4.      * @author lihzh(OneCoder) 
  5.      * @date 2012-6-25 下午10:07:16 
  6.      */ 
  7.     private static void twoCommonThread() { 
  8.         String girlOneName = "Girl One"
  9.         Thread girlOne = new Thread(new MyRunner(3000, girlOneName), girlOneName); 
  10.         String girlTwoName = "Girl Two"
  11.         Thread girlTwo = new Thread(new MyRunner(5000, girlTwoName), girlTwoName); 
  12.         girlOne.start(); 
  13.         System.out.println(girlOneName + "is starting."); 
  14.         girlTwo.start(); 
  15.         System.out.println(girlTwoName + "is starting"); 
  16.     } 
  17.  
  18.     private static class MyRunner implements Runnable { 
  19.          
  20.         private long sleepPeriod; 
  21.         private String threadName; 
  22.          
  23.         public MyRunner(long sleepPeriod, String threadName) { 
  24.             this.sleepPeriod = sleepPeriod; 
  25.             this.threadName = threadName; 
  26.         } 
  27.         @Override 
  28.         public void run() { 
  29.             try { 
  30.                 Thread.sleep(sleepPeriod); 
  31.             } catch (InterruptedException e) { 
  32.                 e.printStackTrace(); 
  33.             } 
  34.             System.out.println(threadName + " has finished."); 
  35.         } 
  36.     } 

 

 

開始都活着。線程

3秒後,妹子1掛了,妹子2活的好好的,她的壽命是5秒。3d

  • 一妹子一王子
  
  
           
  
  
  1. /** 
  2.  * 測試一個用戶一個守護線程 
  3.  *  
  4.  * @author lihzh(OneCoder) 
  5.  * @date 2012-6-25 下午10:22:58 
  6.  */ 
  7. private static void oneCommonOneDaemonThread() { 
  8.     String girlName = "Girl"
  9.     Thread girl = new Thread(new MyRunner(3000, girlName), girlName); 
  10.     String princeName = "Prince"
  11.     Thread prince = new Thread(new MyRunner(5000, princeName), princeName); 
  12.     girl.start(); 
  13.     System.out.println(girlName + "is starting."); 
  14.     prince.setDaemon(true); 
  15.     prince.start(); 
  16.     System.out.println(prince + "is starting"); 

 

開始快樂的生活着,妹子能活3秒,王子原本能活5秒。code

可是3秒後,妹子掛了,王子也殉情了。orm

 

你可能會問,若是王子活3秒,妹子能活5秒呢。我只能說,雖然你是王子,該掛也得掛,妹子還會找到其餘高富帥的,懂?對象

看,王子已經掛了。

  • 兩個王子
  
  
           
  
  
  1.    /** 
  2.  * 測試兩個守護線程 
  3.  *  
  4.  * @author lihzh(OneCoder) 
  5.  * @date 2012-6-25 下午10:29:18 
  6.  */ 
  7. private static void twoDaemonThread() { 
  8.     String princeOneName = "Prince One"
  9.     Thread princeOne = new Thread(new MyRunner(5000, princeOneName), princeOneName); 
  10.     String princeTwoName = "Prince Two"
  11.     Thread princeTwo = new Thread(new MyRunner(3000, princeTwoName), princeTwoName); 
  12.     princeOne.setDaemon(true); 
  13.     princeOne.start(); 
  14.     System.out.println(princeOneName + "is starting."); 
  15.     princeTwo.setDaemon(true); 
  16.     princeTwo.start(); 
  17.     System.out.println(princeTwoName + "is starting"); 

 

 

我只能說,沒有妹子,沒有活着的理由,直接就都掛了。
 
如今,你懂守護線程了嗎?
相關文章
相關標籤/搜索