轉:【Java並發編程】之二:線程中斷(含代碼)

來源:互聯網
上載者:User

標籤:lan   runnable   extend   read   sed   使用   ble   normal   dex   

轉載請註明出處:http://blog.csdn.net/ns_code/article/details/17091267


使用interrupt()中斷線程

    當一個線程運行時,另一個線程可以調用對應的Thread對象的interrupt()方法來中斷它,該方法只是在目標線程中設定一個標誌,表示它已經被中斷,並立即返回。這裡需要注意的是,如果只是單純的調用interrupt()方法,線程並沒有實際被中斷,會繼續往下執行。

    下面一段代碼示範了休眠線程的中斷:

[java] view plain copy
  1. public class SleepInterrupt extends Object implements Runnable{  
  2.     public void run(){  
  3.         try{  
  4.             System.out.println("in run() - about to sleep for 20 seconds");  
  5.             Thread.sleep(20000);  
  6.             System.out.println("in run() - woke up");  
  7.         }catch(InterruptedException e){  
  8.             System.out.println("in run() - interrupted while sleeping");  
  9.             //處理完中斷異常後,返回到run()方法人口,  
  10.             //如果沒有return,線程不會實際被中斷,它會繼續列印下面的資訊  
  11.             return;    
  12.         }  
  13.         System.out.println("in run() - leaving normally");  
  14.     }  
  15.   
  16.   
  17.     public static void main(String[] args) {  
  18.         SleepInterrupt si = new SleepInterrupt();  
  19.         Thread t = new Thread(si);  
  20.         t.start();  
  21.         //主線程休眠2秒,從而確保剛才啟動的線程有機會執行一段時間  
  22.         try {  
  23.             Thread.sleep(2000);   
  24.         }catch(InterruptedException e){  
  25.             e.printStackTrace();  
  26.         }  
  27.         System.out.println("in main() - interrupting other thread");  
  28.         //中斷線程t  
  29.         t.interrupt();  
  30.         System.out.println("in main() - leaving");  
  31.     }  
  32. }  

    運行結果如下:


     主線程啟動新線程後,自身休眠2秒鐘,允許新線程獲得已耗用時間。新線程列印資訊“about to sleep for 20 seconds”後,繼而休眠20秒鐘,大約2秒鐘後,main線程通知新線程中斷,那麼新線程的20秒的休眠將被打斷,從而拋出InterruptException異常,執行跳轉到catch塊,列印出“interrupted while sleeping”資訊,並立即從run()方法返回,然後消亡,而不會列印出catch塊後面的“leaving normally”資訊。    請注意:由於不確定的線程規劃,運行結果的後兩行可能順序相反,這取決於主線程和新線程哪個先消亡。但前兩行資訊的順序必定如所示。    另外,如果將catch塊中的return語句注釋掉,則線程在拋出異常後,會繼續往下執行,而不會被中斷,從而會列印出”leaving normally“資訊。

待決中斷

    在上面的例子中,sleep()方法的實現檢查到休眠線程被中斷,它會相當友好地終止線程,並拋出InterruptedException異常。另外一種情況,如果線程在調用sleep()方法前被中斷,那麼該中斷稱為待決中斷,它會在剛調用sleep()方法時,立即拋出InterruptedException異常。

    下面的代碼示範了待決中斷:

 

[java] view plain copy
  1. public class PendingInterrupt extends Object {  
  2.     public static void main(String[] args){  
  3.         //如果輸入了參數,則在mian線程中中斷當前線程(亦即main線程)  
  4.         if( args.length > 0 ){  
  5.             Thread.currentThread().interrupt();  
  6.         }   
  7.         //擷取目前時間  
  8.         long startTime = System.currentTimeMillis();  
  9.         try{  
  10.             Thread.sleep(2000);  
  11.             System.out.println("was NOT interrupted");  
  12.         }catch(InterruptedException x){  
  13.             System.out.println("was interrupted");  
  14.         }  
  15.         //計算中間代碼執行的時間  
  16.         System.out.println("elapsedTime=" + ( System.currentTimeMillis() - startTime));  
  17.     }  
  18. }  

    如果PendingInterrupt不帶任何命令列參數,那麼線程不會被中斷,最終輸出的時間差距應該在2000附近(具體時間由系統決定,不精確),如果PendingInterrupt帶有命令列參數,則調用中斷當前線程的代碼,但main線程仍然運行,最終輸出的時間差距應該遠小於2000,因為線程尚未休眠,便被中斷,因此,一旦調用sleep()方法,會立即列印出catch塊中的資訊。執行結果如下:

    這種模式下,main線程中斷它自身。除了將中斷標誌(它是Thread的內部標誌)設定為true外,沒有其他任何影響。線程被中斷了,但main線程仍然運行,main線程繼續監視系統時鐘,並進入try塊,一旦調用sleep()方法,它就會注意到待決中斷的存在,並拋出InterruptException。於是執行跳轉到catch塊,並列印出線程被中斷的資訊。最後,計算並列印出時間差。

 

使用isInterrupted()方法判斷中斷狀態   可以在Thread對象上調用isInterrupted()方法來檢查任何線程的中斷狀態。這裡需要注意:線程一旦被中斷,isInterrupted()方法便會返回true,而一旦sleep()方法拋出異常,它將清空中斷標誌,此時isInterrupted()方法將返回false。
   下面的代碼示範了isInterrupted()方法的使用:
[java] view plain copy
  1. public class InterruptCheck extends Object{  
  2.     public static void main(String[] args){  
  3.         Thread t = Thread.currentThread();  
  4.         System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());  
  5.         //待決中斷,中斷自身  
  6.         t.interrupt();  
  7.         System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());  
  8.         System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted());  
  9.   
  10.         try{  
  11.             Thread.sleep(2000);  
  12.             System.out.println("was NOT interrupted");  
  13.         }catch( InterruptedException x){  
  14.             System.out.println("was interrupted");  
  15.         }  
  16.         //拋出異常後,會清除中斷標誌,這裡會返回false  
  17.         System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());  
  18.     }  
  19. }  
    運行結果如下:

 

使用Thread.interrupted()方法判斷中斷狀態

    可以使用Thread.interrupted()方法來檢查當前線程的中斷狀態(並隱式重設為false)。又由於它是靜態方法,因此不能在特定的線程上使用,而只能報告調用它的線程的中斷狀態,如果線程被中斷,而且中斷狀態尚不清楚,那麼,這個方法返回true。與isInterrupted()不同,它將自動重設中斷狀態為false,第二次調用Thread.interrupted()方法,總是返回false,除非中斷了線程。

    如下代碼示範了Thread.interrupted()方法的使用:

 

[java] view plain copy
  1. public class InterruptReset extends Object {  
  2.     public static void main(String[] args) {  
  3.         System.out.println(  
  4.             "Point X: Thread.interrupted()=" + Thread.interrupted());  
  5.         Thread.currentThread().interrupt();  
  6.         System.out.println(  
  7.             "Point Y: Thread.interrupted()=" + Thread.interrupted());  
  8.         System.out.println(  
  9.             "Point Z: Thread.interrupted()=" + Thread.interrupted());  
  10.     }  
  11. }  

    運行結果如下:

    從結果中可以看出,當前線程中斷自身後,在Y點,中斷狀態為true,並由Thread.interrupted()自動重設為false,那麼下次調用該方法得到的結果便是false。


補充    這裡補充下yield和join方法的使用。
    join方法用線程對象調用,如果在一個線程A中調用另一個線程B的join方法,線程A將會等待線程B執行完畢後再執行。    yield可以直接用Thread類調用,yield讓出CPU執行權給同等級的線程,如果沒有相同層級的線程在等待CPU的執行權,則該線程繼續執行。

轉:【Java並發編程】之二:線程中斷(含代碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.