Java多線程中的Interrupt簡(jiǎn)析
Interrupt系列
在調(diào)用如下方法進(jìn)行阻塞的線程,都可以調(diào)用該線程的interrupt()方法打斷其阻塞
- Object的wait方法
- Thread的sleep方法
- Thread的join方法
- InterruptibleChannel的io操作
- Selector的wakeup方法
上述方法統(tǒng)稱為可中斷方法,實(shí)際上,能拋出InterruptedException異常的方法都是可中斷方法
interrupt()方法
//源碼 public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (blockerLock) { Interruptible b = blocker; if (b != null) { interrupt0(); // Just to set the interrupt flag b.interrupt(this); return; } } interrupt0(); }
當(dāng)一個(gè)線程被別的線程調(diào)用它的阻塞方法時(shí),它會(huì)調(diào)用interrupt0()設(shè)置一個(gè)中斷標(biāo)識(shí),如果被interrupt的線程正在阻塞狀態(tài),該線程的阻塞狀態(tài)會(huì)被中斷并且中斷標(biāo)識(shí)被清除,如例一
//例一 package online.hengtian.Thread; import java.util.concurrent.TimeUnit; public class InterruptDemo { public static void main(String[] args){ Thread t1=new Thread(()->{ int i=0; while(i<5){ System.out.println(i+" : 我當(dāng)前的中斷狀態(tài)"+Thread.currentThread().isInterrupted()); try { TimeUnit.SECONDS.sleep(1); System.out.println(i+" : 我沒(méi)被中斷"); } catch (InterruptedException e) { System.out.println("我被中斷了"); System.out.println(i+" : 此時(shí)我的中斷狀態(tài)是"+Thread.currentThread().isInterrupted()); } i++; } }); t1.start(); t1.interrupt(); } }
輸出
0 : 我當(dāng)前的中斷狀態(tài)true
我被中斷了
0 : 此時(shí)我的中斷狀態(tài)是false
1 : 我當(dāng)前的中斷狀態(tài)false
1 : 我沒(méi)被中斷
2 : 我當(dāng)前的中斷狀態(tài)false
2 : 我沒(méi)被中斷
3 : 我當(dāng)前的中斷狀態(tài)false
3 : 我沒(méi)被中斷
4 : 我當(dāng)前的中斷狀態(tài)false
4 : 我沒(méi)被中斷
如果被interrupt的線程并沒(méi)有進(jìn)入阻塞狀態(tài),該線程在進(jìn)入阻塞狀態(tài)后會(huì)立即被中斷,然后清除中斷狀態(tài),測(cè)試如下:
//當(dāng)線程被interrupt之后才會(huì)進(jìn)入sleep方法 package online.hengtian.Thread; import java.util.concurrent.TimeUnit; public class InterruptDemo { public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(()->{ //changed beginning while(!Thread.currentThread().isInterrupted()){ System.out.println("我當(dāng)前沒(méi)被中斷"); } //changed end int i=0; while(i<5){ System.out.println(i+" : 我當(dāng)前的中斷狀態(tài)"+Thread.currentThread().isInterrupted()); try { TimeUnit.SECONDS.sleep(1); System.out.println(i+" : 我沒(méi)被中斷"); } catch (InterruptedException e) { System.out.println("我被中斷了"); System.out.println(i+" : 此時(shí)我的中斷狀態(tài)是"+Thread.currentThread().isInterrupted()); } i++; } }); t1.start(); TimeUnit.SECONDS.sleep(2); t1.interrupt(); } }
截取部分輸出如下
我當(dāng)前沒(méi)被中斷
我當(dāng)前沒(méi)被中斷
我當(dāng)前沒(méi)被中斷
我當(dāng)前沒(méi)被中斷
我當(dāng)前沒(méi)被中斷
我當(dāng)前沒(méi)被中斷
我當(dāng)前沒(méi)被中斷
我當(dāng)前沒(méi)被中斷
0 : 我當(dāng)前的中斷狀態(tài)true
我被中斷了
0 : 此時(shí)我的中斷狀態(tài)是false
1 : 我當(dāng)前的中斷狀態(tài)false
1 : 我沒(méi)被中斷
2 : 我當(dāng)前的中斷狀態(tài)false
2 : 我沒(méi)被中斷
3 : 我當(dāng)前的中斷狀態(tài)false
3 : 我沒(méi)被中斷
4 : 我當(dāng)前的中斷狀態(tài)false
4 : 我沒(méi)被中斷
當(dāng)理解了interrupt方法中斷的根本原因是中斷標(biāo)識(shí)之后,一切都會(huì)變的很簡(jiǎn)單
到此這篇關(guān)于Java多線程中的Interrupt簡(jiǎn)析的文章就介紹到這了,更多相關(guān)Java的Interrupt內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何在 Spring Boot 中配置和使用 CSRF 保護(hù)
CSRF是一種網(wǎng)絡(luò)攻擊,它利用已認(rèn)證用戶的身份來(lái)執(zhí)行未經(jīng)用戶同意的操作,Spring Boot 提供了內(nèi)置的 CSRF 保護(hù)機(jī)制,可以幫助您防止這種類型的攻擊,這篇文章主要介紹了Spring?Boot?中的?CSRF?保護(hù)配置的使用方法,需要的朋友可以參考下2023-09-09詳解java創(chuàng)建一個(gè)女朋友類(對(duì)象啥的new一個(gè)就是)==建造者模式,一鍵重寫
這篇文章主要介紹了java建造者模式一鍵重寫,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04基于SpringBoot+Mybatis實(shí)現(xiàn)Mysql分表
這篇文章主要為大家詳細(xì)介紹了基于SpringBoot+Mybatis實(shí)現(xiàn)Mysql分表的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04SpringBoot處理全局統(tǒng)一異常的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot處理全局統(tǒng)一異常的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09java 非對(duì)稱加密算法DH實(shí)現(xiàn)詳解
這篇文章主要介紹了java 非對(duì)稱加密算法DH實(shí)現(xiàn)詳解 ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Springboot支持Emoji表情的實(shí)現(xiàn)方法
本文主要介紹了Springboot 支持Emoji 表情,本篇的實(shí)現(xiàn)方式是僅需后端處理,具有一定的參考價(jià)值,需要的朋友可以參考一下。2021-07-07SpringBoot+Maven 多模塊項(xiàng)目的構(gòu)建、運(yùn)行、打包實(shí)戰(zhàn)
這篇文章主要介紹了SpringBoot+Maven 多模塊項(xiàng)目的構(gòu)建、運(yùn)行、打包實(shí)戰(zhàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05