Java多線程之Interrupt中斷線程詳解
https://gitee.com/zture/spring-test/blob/master/multithreading/src/test/java/cn/diswares/blog/InterruptTests.java
二、測試為了方便理解簡介中 interrupt 的概念, 寫個 DEMO 測試一下
/** * 調(diào)用 interrupt 并不會影響線程正常運(yùn)行 */@Testpublic void testInvokeInterrupt() throws InterruptedException { Thread t1 = new Thread(() -> { for (int i = 0; ; i++) { log.info(i + ''); } }); t1.start(); // 確保 t1.start() 成功執(zhí)行 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());}三、執(zhí)行過程描述 首先 main 線程中啟動 t1線程 t1 線程死循環(huán)輸出 i++ main 線程確保 t1.start() 執(zhí)行后 打印 t1 線程的線程中斷狀態(tài) 調(diào)用 t1.interrupt() 方法使線程中斷 打印 t1 線程的線程中斷狀態(tài)四、輸出日志
ignore logs ......20:29:57.632 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256120:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256220:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256320:29:57.486 [main] INFO cn.diswares.blog.interrupt.InterruptTests - interrupt 前 t1 interrupt 狀態(tài) = false20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256420:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256520:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256620:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256720:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256820:29:57.633 [main] INFO cn.diswares.blog.interrupt.InterruptTests - interrupt 后 t1 interrupt 狀態(tài) = true20:29:57.633 [main] INFO cn.diswares.blog.interrupt.InterruptTests - t1 是否存活 = true20:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 256920:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 257020:29:57.633 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - 2571ignore logs ......
現(xiàn)象描述
調(diào)用 t1.interrupt() 執(zhí)行前線程的 interrupt 狀態(tài)為 false 調(diào)用 t1.interrupt() 執(zhí)行后線程的 interrupt 狀態(tài)為 true 線程并沒有被中斷, 可以成功死循環(huán)輸出循環(huán)次數(shù)五、結(jié)論Interrupt 的真正作用是給線程對象設(shè)置一個中斷標(biāo)記, 并不會影響線程的正常運(yùn)行
六、主要方法釋義new Thread().interrupt()
中斷此線程(此線程不一定是當(dāng)前線程,而是指調(diào)用該方法的Thread實例所代表的線程),但實際上只是給線程設(shè)置一個中斷標(biāo)志,線程仍會繼續(xù)運(yùn)行。
Thread.interrupted()
注意: 這是個靜態(tài)方法測試當(dāng)前線程是否被中斷(檢查中斷標(biāo)志), 返回一個當(dāng)前線程的 interrupt 狀態(tài), 并重置.當(dāng)我們第二次調(diào)用時中斷狀態(tài)已經(jīng)被重置, 將返回一個false為了方便理解. 寫一個 DEMO
七、DEMODEMO 非常簡單, 調(diào)用兩次 Thread.interrupted() 觀察 main 線程的 interrupt 標(biāo)記
/** * 二次調(diào)用 t1.interrupted() */@Testpublic 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 = true21:06:33.402 [main] INFO cn.diswares.blog.interrupt.InterruptTests - interrupted2 = false
八、拓展程序由于是靜態(tài)方法. 我們來看一下另一個小程序.
跟之前一樣將 t1 程序中斷 調(diào)用 t1.interrupted() 注意這里是個靜態(tài)方法/** * 在主線程中調(diào)用 t1.interrupted() */@Testpublic 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 live21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live21:11:20.490 [main] INFO cn.diswares.blog.interrupt.InterruptTests - false21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is live21:11:20.504 [Thread-1] INFO cn.diswares.blog.interrupt.InterruptTests - t1 is liveignore logs ......
拓展程序結(jié)論
Thread.interrupted() 方法是靜態(tài)方法 它的實現(xiàn)為 Thread.currentThread(), 獲取的是當(dāng)前正在執(zhí)行的線程, JDK 原文注釋如下Returns a reference to the currently executing thread object.
Returns: the currently executing thread.
所以這里 t1.interrupted() 返回的其實是 main 線程的線程中斷標(biāo)記new Thread().isInterrupted()
返回線程對象的中斷標(biāo)記, 不會改變中斷標(biāo)記
true: 中斷標(biāo)記存在 false: 未設(shè)置中斷標(biāo)記狀態(tài)優(yōu)雅的結(jié)束一個線程
在 Java 中結(jié)束一個線程一般有下面三種手段:
(禁用) Thread.stop() 這個方法已經(jīng)被廢棄. 因為這種結(jié)束線程的方式過于暴力. 會將當(dāng)前線程暴力終結(jié). 同時線程持有的鎖也都會釋放, 并且用戶有任何額外的處理來控制, 會導(dǎo)致數(shù)據(jù)不一致 volatile: 外部申明 volatile 開關(guān)變量, 當(dāng)開關(guān)條件不滿足時結(jié)束 (推薦) interrupt: 最優(yōu)雅的方案九、實戰(zhàn)最初的 DEMO 是個死循環(huán), 那我們對其改造一下. 讓它能夠優(yōu)雅的結(jié)束
/** * 調(diào)用 interrupt 并不會影響線程正常運(yùn)行 */@Testpublic 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() 成功執(zhí)行 TimeUnit.SECONDS.sleep(1); t1.interrupt(); TimeUnit.SECONDS.sleep(1); log.info(t1.getState().toString());}
到此這篇關(guān)于Java多線程之Interrupt中斷線程詳解的文章就介紹到這了,更多相關(guān)Java Interrupt中斷線程內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
