Java併發編程(2):線程中斷(含代碼)

使用interrupt()中斷線程

當一個線程運行時,另外一個線程能夠調用對應的Thread對象的interrupt()方法來中斷它,該方法只是在目標線程中設置一個標誌,表示它已經被中斷,並當即返回。這裏須要注意的是,若是隻是單純的調用interrupt()方法,線程並無實際被中斷,會繼續往下執行。sql

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

public class SleepInterrupt extends Object implements Runnable{
 public void run(){
 try{
 System.out.println("in run() - about to sleep for 20 seconds");
 Thread.sleep(20000);
 System.out.println("in run() - woke up");
 }catch(InterruptedException e){
 System.out.println("in run() - interrupted while sleeping");
 //處理完中斷異常後,返回到run()方法人口,
 //若是沒有return,線程不會實際被中斷,它會繼續打印下面的信息
 return; 
 }
 System.out.println("in run() - leaving normally");
 }
 
 public static void main(String[] args) {
 SleepInterrupt si = new SleepInterrupt();
 Thread t = new Thread(si);
 t.start();
 //主線程休眠2秒,從而確保剛纔啓動的線程有機會執行一段時間
 try {
 Thread.sleep(2000); 
 }catch(InterruptedException e){
 e.printStackTrace();
 }
 System.out.println("in main() - interrupting other thread");
 //中斷線程t
 t.interrupt();
 System.out.println("in main() - leaving");
 }
}

運行結果以下:併發

主線程啓動新線程後,自身休眠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異常。學習

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

 

public class PendingInterrupt extends Object {
 public static void main(String[] args){
 //若是輸入了參數,則在mian線程中中斷當前線程(亦即main線程)
 if( args.length > 0 ){
 Thread.currentThread().interrupt();
 } 
 //獲取當前時間
 long startTime = System.currentTimeMillis();
 try{
 Thread.sleep(2000);
 System.out.println("was NOT interrupted");
 }catch(InterruptedException x){
 System.out.println("was interrupted");
 }
 //計算中間代碼執行的時間
 System.out.println("elapsedTime=" + ( System.currentTimeMillis() - startTime));
 }
}

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

歡迎工做一到五年的Java工程師朋友們加入Java架構開發
: 867748702
羣內提供免費的Java架構學習資料(裏面有高可用、高併發、高性能及分佈式、
Jvm性能調優、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,
Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)
合理利用本身每一分每一秒的時間來學習提高本身,
不要再用"沒有時間「來掩飾本身思想上的懶惰!趁年輕,使勁拼,給將來的本身一個交代!線程

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

使用isInterrupted()方法判斷中斷狀態

能夠在Thread對象上調用isInterrupted()方法來檢查任何線程的中斷狀態。這裏須要注意:線程一旦被中斷,isInterrupted()方法便會返回true,而一旦sleep()方法拋出異常,它將清空中斷標誌,此時isInterrupted()方法將返回false。

下面的代碼演示了isInterrupted()方法的使用:

 

public class InterruptCheck extends Object{
 public static void main(String[] args){
 Thread t = Thread.currentThread();
 System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
 //待決中斷,中斷自身
 t.interrupt();
 System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
 System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted());
 
 try{
 Thread.sleep(2000);
 System.out.println("was NOT interrupted");
 }catch( InterruptedException x){
 System.out.println("was interrupted");
 }
 //拋出異常後,會清除中斷標誌,這裏會返回false
 System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
 }
}

運行結果以下:

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

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

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

 

public class InterruptReset extends Object {
 public static void main(String[] args) {
 System.out.println(
 "Point X: Thread.interrupted()=" + Thread.interrupted());
 Thread.currentThread().interrupt();
 System.out.println(
 "Point Y: Thread.interrupted()=" + Thread.interrupted());
 System.out.println(
 "Point Z: Thread.interrupted()=" + Thread.interrupted());
 }

}

運行結果以下:

從結果中能夠看出,當前線程中斷自身後,在Y點,中斷狀態爲true,並由Thread.interrupted()自動重置爲false,那麼下次調用該方法獲得的結果即是false。

補充

這裏補充下yield和join方法的使用。

join方法用線程對象調用,若是在一個線程A中調用另外一個線程B的join方法,線程A將會等待線程B執行完畢後再執行。

yield能夠直接用Thread類調用,yield讓出CPU執行權給同等級的線程,若是沒有相同級別的線程在等待CPU的執行權,則該線程繼續執行。

相關文章
相關標籤/搜索