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

