下面的這斷代碼大家應(yīng)該再熟悉不過(guò)了,線程休眠需要捕獲或者拋出線程中斷異常,也就是你在睡覺(jué)的時(shí)候突然有個(gè)人沖進(jìn)來(lái)把你吵醒了。
try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }
此時(shí)線程被打斷后,代碼會(huì)繼續(xù)運(yùn)行或者拋出異常結(jié)束運(yùn)行,這并不是我們需要的中斷線程的作用。
到底是什么是線程中斷?
線程中斷即線程運(yùn)行過(guò)程中被其他線程給打斷了,它與 stop 最大的區(qū)別是:stop 是由系統(tǒng)強(qiáng)制終止線程,而線程中斷則是給目標(biāo)線程發(fā)送一個(gè)中斷信號(hào),如果目標(biāo)線程沒(méi)有接收線程中斷的信號(hào)并結(jié)束線程,線程則不會(huì)終止,具體是否退出或者執(zhí)行其他邏輯由目標(biāo)線程決定。
我們來(lái)看下線程中斷最重要的 3 個(gè)方法,它們都是來(lái)自 Thread 類!
1、JAVA.lang.Thread#interrupt
中斷目標(biāo)線程,給目標(biāo)線程發(fā)一個(gè)中斷信號(hào),線程被打上中斷標(biāo)記。
2、java.lang.Thread#isInterrupted()
判斷目標(biāo)線程是否被中斷,不會(huì)清除中斷標(biāo)記。
3、java.lang.Thread#interrupted
判斷目標(biāo)線程是否被中斷,會(huì)清除中斷標(biāo)記。
線程中斷實(shí)戰(zhàn)
我們來(lái)實(shí)例演示下線程中斷如何用!
示例1(中斷失敗)
private static void test1() { Thread thread = new Thread(() -> { while (true) { Thread.yield(); } }); thread.start(); thread.interrupt(); }
請(qǐng)問(wèn)示例1中的線程會(huì)被中斷嗎?答案:不會(huì),因?yàn)殡m然給線程發(fā)出了中斷信號(hào),但程序中并沒(méi)有響應(yīng)中斷信號(hào)的邏輯,所以程序不會(huì)有任何反應(yīng)。
示例2:(中斷成功)
private static void test2() { Thread thread = new Thread(() -> { while (true) { Thread.yield(); // 響應(yīng)中斷 if (Thread.currentThread().isInterrupted()) { System.out.println("線程被中斷,程序退出。"); return; } } }); thread.start(); thread.interrupt(); }
我們給示例2加上了響應(yīng)中斷的邏輯,程序接收到中斷信號(hào)打印出信息后返回退出。
示例3(中斷失敗)
private static void test3() throws InterruptedException { Thread thread = new Thread(() -> { while (true) { // 響應(yīng)中斷 if (Thread.currentThread().isInterrupted()) { System.out.println("線程被中斷,程序退出。"); return; } try { Thread.sleep(3000); } catch (InterruptedException e) { System.out.println("線程休眠被中斷,程序退出。"); } } }); thread.start(); Thread.sleep(2000); thread.interrupt(); }
示例3 sleep() 方法被中斷,并輸出了 線程休眠被中斷,程序退出。 程序繼續(xù)運(yùn)行……為什么呢?
來(lái)看 sleep 的源碼:

可以看出 sleep() 方法被中斷后會(huì)清除中斷標(biāo)記,所以循環(huán)會(huì)繼續(xù)運(yùn)行。。
示例4(中斷成功)
private static void test4() throws InterruptedException { Thread thread = new Thread(() -> { while (true) { // 響應(yīng)中斷 if (Thread.currentThread().isInterrupted()) { System.out.println("線程被中斷,程序退出。"); return; } try { Thread.sleep(3000); } catch (InterruptedException e) { System.out.println("線程休眠被中斷,程序退出。"); Thread.currentThread().interrupt(); } } }); thread.start(); Thread.sleep(2000); thread.interrupt(); }
示例4全部信息輸出并正常退出,只是在 sleep() 方法被中斷并清除標(biāo)記后手動(dòng)重新中斷當(dāng)前線程,然后程序接收中斷信號(hào)返回退出