基于JDK8總結(jié)java中的interrupt
1. interrupt知識(shí)點(diǎn)
以下總結(jié)基于JDK8
本文不會(huì)完整說明interrupt,只會(huì)羅列一些比較重要的點(diǎn)。完整了解Thread.interrupt可以看參考資料。
以下的一些理解新的有助于理解參考資料的文章:
interrupt方法調(diào)用后,針對(duì)BLOCKED狀態(tài)的線程,只是設(shè)定中斷標(biāo)志位為true。是否響應(yīng)中斷(感知這個(gè)標(biāo)志位的變化)取決于API的設(shè)計(jì)。JDK的阻塞IO API、Synchronized同步塊、還有Lock中的很多方法(不包括lockInterruptibly)都是不響應(yīng)中斷的。當(dāng)然調(diào)用線程可以利用標(biāo)志位判斷來使得自己設(shè)計(jì)的API是可響應(yīng)中斷的。
interrupt方法調(diào)用后,針對(duì)WAITING/TIMED_WAITING狀態(tài)的線程,會(huì)上拋interruptedException**并且設(shè)置中斷標(biāo)志位false**。例如線程調(diào)用Thread.sleep,Object.wait()之后。
如果線程尚未啟動(dòng)(NEW),或者已經(jīng)結(jié)束(TERMINATED),則調(diào)用interrupt()對(duì)它沒有任何效果,中斷標(biāo)志位也不會(huì)被設(shè)置。
最佳實(shí)踐:有時(shí)候一些方法設(shè)計(jì)上不允許被中斷或者取消,但是當(dāng)別的線程發(fā)來中斷請(qǐng)求的時(shí)候,也需要進(jìn)行標(biāo)記的保留,方便其他調(diào)用方“了解情況”
public Task getNextTask(BlockingQueue<Task> queue) { boolean interrupted = false; try { while (true) { try { return queue.take(); } catch (InterruptedException e) { //fianlly中依賴的狀態(tài)標(biāo)記 interrupted = true; // fall through and retry } } } finally { if (interrupted) //在fianlly中重新標(biāo)記,確保沒有丟失中斷通知 Thread.currentThread().interrupt(); } }
利用中斷可以實(shí)現(xiàn)一些cancel的操作。例如:
package concurrent; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Created by wanshao * Date: 2017/12/18 * Time: 下午3:42 **/ public class InterruptExample { public static void main(String[] args) throws InterruptedException { InterruptTask interruptTask = new InterruptTask(); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(interruptTask); Thread.sleep(100); interruptTask.cancel(); executorService.shutdown(); } } /** * 一個(gè)響應(yīng)中斷的任務(wù) */ class InterruptTask implements Callable<Integer> { private BlockingQueue<Task> queue; //保存要被interrupt的線程 Thread t; @Override public Integer call() throws InterruptedException { System.out.println("start a blocked task"); try { t = Thread.currentThread(); Thread.currentThread().sleep(50000); } catch (InterruptedException e) { System.out.println("be interrupted"); e.printStackTrace(); } return 0; } public void cancel() { System.out.println("cacel a task...."); //這里直接調(diào)用Thread.currentThread()會(huì)獲取到main線程,而不是線程池里面的線程 if (!t.isInterrupted()) { t.interrupt(); } } }
總結(jié)
以上所述是小編給大家介紹的基于JDK8總結(jié)java中的interrupt,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot整合任務(wù)系統(tǒng)quartz和SpringTask的方法
這篇文章主要介紹了SpringBoot整合任務(wù)系統(tǒng)(quartz和SpringTask),Quartz是一個(gè)比較成熟了的定時(shí)任務(wù)框架,但是捏,它稍微的有些許繁瑣,本文先給大家講解下Quartz的一些基本概念結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10使用JVMTI實(shí)現(xiàn)SpringBoot的jar加密,防止反編譯
這篇文章主要介紹了使用JVMTI實(shí)現(xiàn)SpringBoot的jar加密,防止反編譯問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08JavaWeb開發(fā)之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JC
這篇文章主要介紹了JavaWeb開發(fā)之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基礎(chǔ)框架的相關(guān)資料,需要的朋友可以參考下2016-12-12IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn)
本文主要介紹了IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01MyBatis的@SelectProvider注解構(gòu)建動(dòng)態(tài)SQL方式
這篇文章主要介紹了MyBatis的@SelectProvider注解構(gòu)建動(dòng)態(tài)SQL方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08在controller中如何設(shè)置接收參數(shù)的默認(rèn)值
這篇文章主要介紹了在controller中如何設(shè)置接收參數(shù)的默認(rèn)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Springboot整合Shiro實(shí)現(xiàn)登錄與權(quán)限校驗(yàn)詳細(xì)解讀
本文給大家介紹Springboot整合Shiro的基本使用,Apache?Shiro是Java的一個(gè)安全框架,Shiro本身無法知道所持有令牌的用戶是否合法,我們將整合Shiro實(shí)現(xiàn)登錄與權(quán)限的驗(yàn)證2022-04-04