- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Java多線(xiàn)程之Interrupt中斷線(xiàn)程詳解
為了方便理解簡(jiǎn)介中 interrupt 的概念, 寫(xiě)個(gè) DEMO 測試一下
/** * 調用 interrupt 并不會(huì )影響線(xiàn)程正常運行 */ @Test public void testInvokeInterrupt() throws InterruptedException { Thread t1 = new Thread(() -> { for (int i = 0; ; i++) { log.info(i + ""); } }); t1.start(); // 確保 t1.start() 成功執行 Thread.sleep(1); log.info("interrupt 前 t1 interrupt 狀態(tài) = {}", t1.isInterrupted()); t1.interrupt(); log.info("interrupt 后 t1 interrupt 狀態(tài) = {}", t1.isInterrupted()); log.info("t1 是否存活 = {}", t1.isAlive()); }
ignore logs ......
20:29:57.632 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2561
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2562
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2563
20:29:57.486 [main] INFO cn.diswares.blog.interrupt.InterruptTests - interrupt 前 t1 interrupt 狀態(tài) = false
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2564
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2565
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2566
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2567
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2568
20:29:57.633 [main] INFO cn.diswares.blog.interrupt.InterruptTests - interrupt 后 t1 interrupt 狀態(tài) = true
20:29:57.633 [main] INFO cn.diswares.blog.interrupt.InterruptTests - t1 是否存活 = true
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2569
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2570
20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2571
ignore logs ......
現象描述
Interrupt 的真正作用是給線(xiàn)程對象設置一個(gè)中斷標記, 并不會(huì )影響線(xiàn)程的正常運行
new Thread().interrupt()
中斷此線(xiàn)程(此線(xiàn)程不一定是當前線(xiàn)程,而是指調用該方法的Thread實(shí)例所代表的線(xiàn)程),但實(shí)際上只是給線(xiàn)程設置一個(gè)中斷標志,線(xiàn)程仍會(huì )繼續運行。
Thread.interrupted()
注意: 這是個(gè)靜態(tài)方法
測試當前線(xiàn)程是否被中斷(檢查中斷標志), 返回一個(gè)當前線(xiàn)程的 interrupt 狀態(tài), 并重置.
當我們第二次調用時(shí)中斷狀態(tài)已經(jīng)被重置, 將返回一個(gè)false
為了方便理解. 寫(xiě)一個(gè) DEMO
DEMO 非常簡(jiǎn)單, 調用兩次 Thread.interrupted() 觀(guān)察 main 線(xiàn)程的 interrupt 標記
/** * 二次調用 t1.interrupted() */ @Test public void testDoubleInvokeInterrupted () throws InterruptedException { Thread.currentThread().interrupt(); log.info("interrupted1 = {}", Thread.interrupted()); log.info("interrupted2 = {}", Thread.interrupted()); }
輸出日志
21:06:33.397 [main] INFO cn.diswares.blog.interrupt.InterruptTests - interrupted1 = true
21:06:33.402 [main] INFO cn.diswares.blog.interrupt.InterruptTests - interrupted2 = false
由于是靜態(tài)方法. 我們來(lái)看一下另一個(gè)小程序.
/** * 在主線(xiàn)程中調用 t1.interrupted() */ @Test public void testMainInterrupted() throws InterruptedException { Thread t1 = new Thread(() -> { for (int i = 0; ; i++) { log.info("t1 is live"); } }); t1.start(); Thread.sleep(1); t1.interrupt(); Thread.sleep(1); log.info("{}", t1.interrupted()); }
拓展程序日志
ignore logs ......
21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live
21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live
21:11:20.490 [main] INFO cn.diswares.blog.interrupt.InterruptTests - false
21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live
21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live
21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live
ignore logs ......
拓展程序結論
Returns a reference to the currently executing thread object.
Returns: the currently executing thread.
new Thread().isInterrupted()
返回線(xiàn)程對象的中斷標記, 不會(huì )改變中斷標記
優(yōu)雅的結束一個(gè)線(xiàn)程
在 Java 中結束一個(gè)線(xiàn)程一般有下面三種手段:
最初的 DEMO 是個(gè)死循環(huán), 那我們對其改造一下. 讓它能夠優(yōu)雅的結束
/** * 調用 interrupt 并不會(huì )影響線(xiàn)程正常運行 */ @Test public void testGracefulEndThread() throws InterruptedException { Thread t1 = new Thread(() -> { for (int i = 0; ; i++) { if (Thread.currentThread().isInterrupted()) { log.info("{} = true, i = {}", Thread.currentThread().getName(), i); break; } else { log.info("{} = false, i = {}", Thread.currentThread().getName(), i); } } }); t1.start(); // 確保 t1.start() 成功執行 TimeUnit.SECONDS.sleep(1); t1.interrupt(); TimeUnit.SECONDS.sleep(1); log.info(t1.getState().toString()); }
到此這篇關(guān)于Java多線(xiàn)程之Interrupt中斷線(xiàn)程詳解的文章就介紹到這了,更多相關(guān)Java Interrupt中斷線(xiàn)程內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站