Java thread.isInterrupted() 返回值不確定結果分析解決
一、代碼
先上代碼(以下這段代碼會有多種執(zhí)行結果)
@Test public void test_interrupted_thread() throws Exception { InterruptThread interruptThread = new InterruptThread(); interruptThread.start(); interruptThread.interrupt(); System.out.println("interruptThread.isInterrupted():" + interruptThread.isInterrupted()); } public class InterruptThread extends Thread { @Override public void run() { for (int i=0; i< 3; i++) { System.out.println("i=" + (i + 1)); } System.out.println("【InterruptThread】結束"); } }
執(zhí)行結果1:
i=1
i=2
i=3
【Thread-0】【InterruptThread】結束
【main】interruptThread.isInterrupted():false
執(zhí)行結果2:
【main】interruptThread.isInterrupted():true
i=1
i=2
i=3
【Thread-0】【InterruptThread】結束
執(zhí)行結果3:
i=1
i=2
i=3
【Thread-0】【InterruptThread】結束
【main】interruptThread.isInterrupted():true
二、分析結果
執(zhí)行結果1:
Main線程調(diào)用了interruptThread.start();,interruptThread線程啟動,執(zhí)行了interruptThread線程內(nèi)容,同時Main線程調(diào)用了interruptThread.interrupt();,設定了interruptThread線程中斷標記為true,最后InterruptThread結束,清除中斷標記,Main線程調(diào)用interruptThread.isInterrupted() 獲取interruptThread線程中斷標記為false。
執(zhí)行結果2:
Main線程調(diào)用了interruptThread.start();,interruptThread線程啟動,但是由于CPU隨機調(diào)度,在執(zhí)行了interruptThread線程內(nèi)容前,先執(zhí)行Main線程調(diào)用interruptThread.interrupt();,設定了interruptThread線程中斷標記為true,且先調(diào)用interruptThread.isInterrupted()獲取interruptThread線程中斷標記為true并輸出,最后在執(zhí)行interruptThread線程內(nèi)容。
執(zhí)行結果3:
Main線程調(diào)用了interruptThread.start();,interruptThread線程啟動,執(zhí)行了interruptThread線程內(nèi)容,同時Main線程調(diào)用了interruptThread.interrupt();,設定了interruptThread線程中斷標記為true,最后InterruptThread結束,但是Main線程調(diào)用interruptThread.isInterrupted() 獲取interruptThread線程中斷標記為true。(與執(zhí)行結果1執(zhí)行順序一致,但是最終結果不一致)
原因分析:
Main線程調(diào)用interruptThread.interrupt()后立即調(diào)用interruptThread.isInterrupted(),雖然interruptThread執(zhí)行結束,但有可能在interruptThread線程還未完成清除打斷標記就Main線程就查看打斷標記,此時仍然為true。
三、解決方案
如果Main線程要得到穩(wěn)定的false,即重置打斷標記后的結果,有如下方案:
(1)需要Main線程在調(diào)用interruptThread.interrupt();,對Main線程sleep一會,給點時間,再通過調(diào)用interruptThread.isInterrupted()獲取interruptThread線程的中斷狀態(tài)。
@Test public void test_interrupted_thread() throws Exception { InterruptThread interruptThread = new InterruptThread(); interruptThread.start(); interruptThread.interrupt(); Thread.sleep(100); System.out.println("【" + Thread.currentThread().getName() + "】" + "interruptThread.isInterrupted():" + interruptThread.isInterrupted()); }
(2)也可以通過Main線程調(diào)用interruptThread.join(),讓Main線程等到interruptThread執(zhí)行直到中止后再調(diào)用interruptThread.isInterrupted()獲取interruptThread線程的中斷狀態(tài)。
@Test public void test_interrupted_thread() throws Exception { InterruptThread interruptThread = new InterruptThread(); interruptThread.start(); interruptThread.interrupt(); interruptThread.join(); System.out.println("【" + Thread.currentThread().getName() + "】" + "interruptThread.isInterrupted():" + interruptThread.isInterrupted()); }
到此這篇關于Java thread.isInterrupted() 返回值不確定結果分析解決的文章就介紹到這了,更多相關Java thread.isInterrupted() 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)簡易版聯(lián)網(wǎng)坦克對戰(zhàn)小游戲(附源碼)
這篇文章主要給大家介紹了關于Java實現(xiàn)簡易版聯(lián)網(wǎng)坦克對戰(zhàn)小游戲的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04spring boot下mybatis配置雙數(shù)據(jù)源的實例
這篇文章主要介紹了spring boot下mybatis配置雙數(shù)據(jù)源的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot如何取消內(nèi)置Tomcat啟動并改用外接Tomcat
這篇文章主要介紹了SpringBoot如何取消內(nèi)置Tomcat啟動并改用外接Tomcat,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11SpringBoot如何在線程中獲取@Service Bean類
這篇文章主要介紹了SpringBoot如何在線程中獲取@Service Bean類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02