聊聊Java 中的線程中斷
Java如何實(shí)現(xiàn)線程中斷?
通過調(diào)用Thread類的實(shí)例方法interrupt。如下:
Thread thread = new Thread(){
@Override
public void run() {
if(isInterrupted()){
System.out.println("interrupt");
}
}
};
thread.start();
thread.interrupt();
線程中斷后線程會(huì)立即停止執(zhí)行嗎?
NO。 而如果線程未阻塞,或未關(guān)心中斷狀態(tài),則線程會(huì)正常執(zhí)行,不會(huì)被打斷。
Thread.interrupt()的官方解釋是這樣的:
If this thread is blocked in an invocation of the
Object#wait() wait(), { Object#wait(long) wait(long)}, or { Object#wait(long, int) wait(long, int)} methods of the { Object} class, or of the { #join()}, { #join(long)}, { #join(long, int)}, { #sleep(long)}, or { #sleep(long, int)}, methods of this class, then its interrupt status will be cleared and it will receive an { InterruptedException}.
也就是:處于阻塞的線程,即在執(zhí)行Object對(duì)象的wait()、wait(long)、wait(long, int),或者線程類的join()、join(long)、join(long, int)、sleep(long)、sleep(long,int)方法后線程的狀態(tài),當(dāng)線程調(diào)用interrupt()方法后,這些方法將拋出InterruptedException異常,并清空線程的中斷狀態(tài)。
比如下面的例子會(huì)中斷兩次,第一次sleep方法收到中斷信號(hào)后拋出了InterruptedException,捕獲異常后中斷狀態(tài)清空,然后繼續(xù)執(zhí)行下一次:
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(){
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("interrupt");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
thread.interrupt();
Thread.sleep(5000);
thread.interrupt();
}
而下面這個(gè)例子則會(huì)一直執(zhí)行,不會(huì)被打斷:
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(){
@Override
public void run() {
while (true)
System.out.println("interrupt");
}
};
thread.start();
thread.interrupt();
}
interrupted與isInterrupted方法啥區(qū)別?
- Thread類的靜態(tài)方法interrupted:測(cè)試當(dāng)前線程是否已經(jīng)中斷。如果線程處于中斷狀態(tài)返回true,否則返回false。同時(shí)該方法將清除的線程的中斷狀態(tài)。即:如果連續(xù)兩次調(diào)用該方法,則第二次調(diào)用將返回false。該方法可用于清除線程中斷狀態(tài)使用。
- Thread類的實(shí)例方法isInterrupted:測(cè)試線程是否已經(jīng)中斷。線程的中斷狀態(tài)不受該方法的影響。
Thread類并沒有提供單獨(dú)清除中斷狀態(tài)的方法,所以有兩種方式來達(dá)到此目的:
- 對(duì)于sleep等阻塞方法,catch InterruptedException異常;
- 調(diào)用Thread類的靜態(tài)方法interrupted
線程中斷有哪些實(shí)際應(yīng)用?
線程中斷的幾個(gè)實(shí)際應(yīng)用場(chǎng)景:
- 在處理Web請(qǐng)求時(shí),可能將請(qǐng)求分配到多個(gè)線程去處理,實(shí)現(xiàn)請(qǐng)求執(zhí)行的超時(shí)機(jī)制;
- 實(shí)現(xiàn)線程池時(shí),關(guān)閉線程池中的線程任務(wù)。
以上就是聊聊Java 中的線程中斷的詳細(xì)內(nèi)容,更多關(guān)于Java 線程中斷的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java策略模式的簡單應(yīng)用實(shí)現(xiàn)方法
這篇文章主要介紹了Java策略模式的簡單應(yīng)用實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
springboot2學(xué)習(xí)世界著名程序springboot開發(fā)體驗(yàn)
這篇文章主要為大家介紹了世界著名程序springboot開發(fā)體驗(yàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
基于spring boot 2和shiro實(shí)現(xiàn)身份驗(yàn)證案例
這篇文章主要介紹了基于spring boot 2和shiro實(shí)現(xiàn)身份驗(yàn)證案例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring源碼剖析之Spring處理循環(huán)依賴的問題
大家都知道循環(huán)依賴依賴指的是Bean與Bean之間的依賴關(guān)系,循環(huán)依賴指的是兩個(gè)或者多個(gè)Bean相互依賴,本文通過代碼示例給大家講解Spring處理循環(huán)依賴的問題,感興趣的朋友一起看看吧2021-06-06
MyBatis中#號(hào)與美元符號(hào)的區(qū)別
#{變量名}可以進(jìn)行預(yù)編譯、類型匹配等操作,#{變量名}會(huì)轉(zhuǎn)化為jdbc的類型。很多朋友不清楚在mybatis中#號(hào)與美元符號(hào)的不同,接下來通過本文給大家介紹兩者的區(qū)別,感興趣的朋友參考下吧2017-01-01

