Java線程中的interrupt詳解
interrupt概念
interrupt翻譯成中文為“打斷”的意思,但實(shí)際上,interrupt()方法并非將一個線程打中斷的意思。查看Thread.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(); }
從以上源碼可以知道,interrupt()方法只是設(shè)置了Thread對象中的一個標(biāo)志位而已(Just to set the interrupt flag)。它的意義在于,線程可以通過這個標(biāo)志位來決定需要做什么操作。
interrupt相關(guān)方法
- interrupt()
打斷某個線程(設(shè)置標(biāo)志位) - isInterrupted()
查詢某個線程是否被打斷過(查詢標(biāo)志位) - interrupted()
查詢當(dāng)前線程是否被打斷過,并重置標(biāo)志位
public class TestThread { public static void main(String[] args) { Thread thread = new Thread(() -> { try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("Thread is interrupted"); System.out.println(Thread.currentThread().isInterrupted()); } }); thread.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } thread.interrupt(); } }
輸出如下:
Thread is interrupted
false
可知當(dāng)線程sleep時,如果調(diào)用了interrupt()方法,將會拋出InterruptedException異常。捕獲異常之后,會重置interrupt標(biāo)志位。
wait、sleep、join與interrupt
當(dāng)一個線程在wait、sleep或join時,如果調(diào)用了其interrupt()方法,則會拋出InterruptedException異常。
synchronized與interrupt
當(dāng)一個線程在等待鎖的時候,其能否被interrupt打斷?
public class TestThread { public static void main(String[] args) { final Object o = new Object(); new Thread(() -> { synchronized (o) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); Thread thread = new Thread(() -> { System.out.println("thread 獲得鎖"); }); thread.start(); thread.interrupt(); } }
輸出:
thread 獲得鎖
可知當(dāng)線程等待鎖時,并不會被打斷。
ReentrantLock與interrupt
public class TestThread { public static void main(String[] args) { Lock lock = new ReentrantLock(); Thread t1 = new Thread(() -> { lock.lock(); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } lock.unlock(); }); Thread t2 = new Thread(() -> { lock.lock(); System.out.println("t2 獲得鎖"); lock.unlock(); }); t1.start(); t2.start(); t2.interrupt(); } }
輸出:
t2 獲得鎖
此時,線程在lock等待時依然不會被打斷。如果希望線程在lock等待時可以被打斷,可使用lockInterruptibly方法。
到此這篇關(guān)于Java線程中的interrupt詳解的文章就介紹到這了,更多相關(guān)線程之interrupt內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus實(shí)現(xiàn)2種分頁方法(QueryWrapper查詢分頁和SQL查詢分頁)
本文主要介紹了MyBatis-Plus實(shí)現(xiàn)2種分頁方法,主要包括QueryWrapper查詢分頁和SQL查詢分頁,具有一定的參考價值,感興趣的可以了解一下2021-08-08Java調(diào)用elasticsearch本地代碼的操作方法
這篇文章主要介紹了Java調(diào)用elasticsearch本地代碼的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04springboot2.x解決運(yùn)行順序及Bean對象注入順序的問題
這篇文章主要介紹了springboot2.x解決運(yùn)行順序及Bean對象注入順序的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-012020最新IDEA SpringBoot整合Dubbo的實(shí)現(xiàn)(zookeeper版)
這篇文章主要介紹了2020最新IDEA SpringBoot整合Dubbo(zookeeper版),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09基于常用json框架介紹和Jackson返回結(jié)果處理方式
這篇文章主要介紹了基于常用json框架介紹和Jackson返回結(jié)果處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09關(guān)于MyBatis結(jié)果映射的實(shí)例總結(jié)
結(jié)果集映射主要是為了解決屬性名和類型名不一致的問題,下面這篇文章主要給大家介紹了關(guān)于MyBatis結(jié)果映射的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05