下面的這斷代碼大家應該再熟悉不過了,線程休眠需要捕獲或者拋出線程中斷異常,也就是你在睡覺的時候突然有個人沖進來把你吵醒了。
1
2
3
4
5
|
try { thread.sleep( 3000 ); } catch (interruptedexception e) { e.printstacktrace(); } |
此時線程被打斷后,代碼會繼續運行或者拋出異常結束運行,這并不是我們需要的中斷線程的作用。
到底是什么是線程中斷?
線程中斷即線程運行過程中被其他線程給打斷了,它與 stop 最大的區別是:stop 是由系統強制終止線程,而線程中斷則是給目標線程發送一個中斷信號,如果目標線程沒有接收線程中斷的信號并結束線程,線程則不會終止,具體是否退出或者執行其他邏輯由目標線程決定。
我們來看下線程中斷最重要的 3 個方法,它們都是來自 thread 類!
1、java.lang.thread#interrupt
中斷目標線程,給目標線程發一個中斷信號,線程被打上中斷標記。
2、java.lang.thread#isinterrupted()
判斷目標線程是否被中斷,不會清除中斷標記。
3、java.lang.thread#interrupted
判斷目標線程是否被中斷,會清除中斷標記。
線程中斷實戰
我們來實例演示下線程中斷如何用!
示例1(中斷失敗)
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 微信公眾號:java技術棧 */ private static void test1() { thread thread = new thread(() -> { while ( true ) { thread.yield(); } }); thread.start(); thread.interrupt(); } |
請問示例1中的線程會被中斷嗎?答案:不會,因為雖然給線程發出了中斷信號,但程序中并沒有響應中斷信號的邏輯,所以程序不會有任何反應。
示例2:(中斷成功)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * 微信公眾號:java技術棧 */ private static void test2() { thread thread = new thread(() -> { while ( true ) { thread.yield(); // 響應中斷 if (thread.currentthread().isinterrupted()) { system.out.println( "java技術棧線程被中斷,程序退出。" ); return ; } } }); thread.start(); thread.interrupt(); } |
我們給示例2加上了響應中斷的邏輯,程序接收到中斷信號打印出信息后返回退出。
示例3(中斷失敗)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/** * 微信公眾號:java技術棧 */ private static void test3() throws interruptedexception { thread thread = new thread(() -> { while ( true ) { // 響應中斷 if (thread.currentthread().isinterrupted()) { system.out.println( "java技術棧線程被中斷,程序退出。" ); return ; } try { thread.sleep( 3000 ); } catch (interruptedexception e) { system.out.println( "java技術棧線程休眠被中斷,程序退出。" ); } } }); thread.start(); thread.sleep( 2000 ); thread.interrupt(); } |
示例3 sleep() 方法被中斷,并輸出了 java技術棧線程休眠被中斷,程序退出。 程序繼續運行……為什么呢?
來看 sleep 的源碼:
可以看出 sleep() 方法被中斷后會清除中斷標記,所以循環會繼續運行。。
示例4(中斷成功)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * 微信公眾號:java技術棧 */ private static void test4() throws interruptedexception { thread thread = new thread(() -> { while ( true ) { // 響應中斷 if (thread.currentthread().isinterrupted()) { system.out.println( "java技術棧線程被中斷,程序退出。" ); return ; } try { thread.sleep( 3000 ); } catch (interruptedexception e) { system.out.println( "java技術棧線程休眠被中斷,程序退出。" ); thread.currentthread().interrupt(); } } }); thread.start(); thread.sleep( 2000 ); thread.interrupt(); } |
示例4全部信息輸出并正常退出,只是在 sleep() 方法被中斷并清除標記后手動重新中斷當前線程,然后程序接收中斷信號返回退出。
通過以上 4 個中斷示例,相信對 java 線程中斷的概念有了全面的了解。