Java線程中的interrupt方法解讀
interrupt方法
首先梳理Thread關(guān)于interrupt的定義
/** * Interrupts this thread. * * <p> Unless the current thread is interrupting itself, which is * always permitted, the {@link #checkAccess() checkAccess} method * of this thread is invoked, which may cause a {@link * SecurityException} to be thrown. * * <p> If this thread is blocked in an invocation of the {@link * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link * Object#wait(long, int) wait(long, int)} methods of the {@link Object} * class, or of the {@link #join()}, {@link #join(long)}, {@link * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, * methods of this class, then its interrupt status will be cleared and it * will receive an {@link InterruptedException}. * * <p> If this thread is blocked in an I/O operation upon an {@link * java.nio.channels.InterruptibleChannel InterruptibleChannel} * then the channel will be closed, the thread's interrupt * status will be set, and the thread will receive a {@link * java.nio.channels.ClosedByInterruptException}. * * <p> If this thread is blocked in a {@link java.nio.channels.Selector} * then the thread's interrupt status will be set and it will return * immediately from the selection operation, possibly with a non-zero * value, just as if the selector's {@link * java.nio.channels.Selector#wakeup wakeup} method were invoked. * * <p> If none of the previous conditions hold then this thread's interrupt * status will be set. </p> * * <p> Interrupting a thread that is not alive need not have any effect. * * @throws SecurityException * if the current thread cannot modify this thread * * @revised 6.0 * @spec JSR-51 */ public void interrupt()
這里講述了Thread實(shí)例對(duì)象調(diào)用方法interrupt,有這幾種情況
? 1.在線程里面有wait,sleep,join方法,會(huì)彈出InterruptedException,狀態(tài)會(huì)被清除
? 2.io里面,會(huì)彈出異常,狀態(tài)會(huì)被設(shè)置,…
? 3.如果線程阻塞在nio中的Selector中,狀態(tài)會(huì)被設(shè)置,…
? 4.如果不是上面,這些情況,狀態(tài)會(huì)被設(shè)置
可以看出interrupt是關(guān)于狀態(tài)的設(shè)置
我們來(lái)驗(yàn)證一下
第一種,沒(méi)有任何情況的
public static void main(String[] args) { Thread t1 = new Thread(()->{ while(true){ if (interrupted()) { break; } System.out.println("i" + new Date()); } }); t1.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } t1.interrupt(); }
線程會(huì)在(1000+)ms后結(jié)束,整個(gè)程序也會(huì)結(jié)束
第二種,有sleep的情況下
public static void main(String[] args) { Thread t1 = new Thread(()->{ while(true){ if (interrupted()) { break; } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("i" + new Date()); } }); t1.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } t1.interrupt(); }
方法并不會(huì)退出,只是打出了一個(gè)InterruptException
如何處理中斷?
- 上文都在介紹如何獲取中斷狀態(tài),那么當(dāng)我們捕獲到中斷狀態(tài)后,究竟如何處理呢?
- Java類庫(kù)中提供的一些可能會(huì)發(fā)生阻塞的方法都會(huì)拋InterruptedException異常,如:BlockingQueue#put、BlockingQueue#take、Object#wait、Thread#sleep。
- 當(dāng)你在某一條線程中調(diào)用這些方法時(shí),這個(gè)方法可能會(huì)被阻塞很長(zhǎng)時(shí)間,你可以在別的線程中調(diào)用當(dāng)前線程對(duì)象的interrupt方法觸發(fā)這些函數(shù)拋出InterruptedException異常。
- 當(dāng)一個(gè)函數(shù)拋出InterruptedException異常時(shí),表示這個(gè)方法阻塞的時(shí)間太久了,別人不想等它執(zhí)行結(jié)束了。
- 當(dāng)你的捕獲到一個(gè)InterruptedException異常后,亦可以處理它,或者向上拋出。
- 拋出時(shí)要注意???:當(dāng)你捕獲到InterruptedException異常后,當(dāng)前線程的中斷狀態(tài)已經(jīng)被修改為false(表示線程未被中斷);此時(shí)你若能夠處理中斷,則不用理會(huì)該值;但如果你繼續(xù)向上拋InterruptedException異常,你需要再次調(diào)用interrupt方法,將當(dāng)前線程的中斷狀態(tài)設(shè)為true。
- 注意:絕對(duì)不能“吞掉中斷”!即捕獲了InterruptedException而不作任何處理。這樣違背了中斷機(jī)制的規(guī)則,別人想讓你線程中斷,然而你自己不處理,也不將中斷請(qǐng)求告訴調(diào)用者,調(diào)用者一直以為沒(méi)有中斷請(qǐng)求。
更新相關(guān)interrupt方法的意義
public void interrupt()
Thread里面的打斷函數(shù),可以使用新建一個(gè)Thread對(duì)象,執(zhí)行thread.interrupt();
修改調(diào)用線程的interrupt 的status
public static boolean interrupted()
該方法可以判斷是否被打斷,但是這個(gè)interrupt的status會(huì)被清除,
/** * Tests whether the current thread has been interrupted. The * <i>interrupted status</i> of the thread is cleared by this method. In * other words, if this method were to be called twice in succession, the * second call would return false (unless the current thread were * interrupted again, after the first call had cleared its interrupted * status and before the second call had examined it). * * <p>A thread interruption ignored because a thread was not alive * at the time of the interrupt will be reflected by this method * returning false. * * @return <code>true</code> if the current thread has been interrupted; * <code>false</code> otherwise. * @see #isInterrupted() * @revised 6.0 */
public boolean isInterrupted()
該方法可以判斷是否被打斷
到此這篇關(guān)于Java線程中的interrupt方法解讀的文章就介紹到這了,更多相關(guān)interrupt方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之二叉堆
二叉堆是一種特殊的堆,其實(shí)質(zhì)是完全二叉樹(shù)。二叉堆有兩種:最大堆和最小堆。最大堆是指父節(jié)點(diǎn)鍵值總是大于或等于任何一個(gè)子節(jié)點(diǎn)的鍵值。而最小堆恰恰相反,指的是父節(jié)點(diǎn)鍵值總是小于任何一個(gè)子節(jié)點(diǎn)的鍵值2022-02-02Java中List轉(zhuǎn)Map的幾種常見(jiàn)方式與對(duì)比
JavaList轉(zhuǎn)Map是一個(gè)非常常用的技術(shù),對(duì)于Java開(kāi)發(fā)人員來(lái)講,掌握該技術(shù)可以幫助我們更加高效地操作List集合中的對(duì)象,這篇文章主要給大家介紹了關(guān)于Java中List轉(zhuǎn)Map的幾種常見(jiàn)方式與對(duì)比的相關(guān)資料,需要的朋友可以參考下2024-02-02Spring?Boot?Admin?添加報(bào)警提醒和登錄驗(yàn)證功能的具體實(shí)現(xiàn)
報(bào)警提醒功能是基于郵箱實(shí)現(xiàn)的,當(dāng)然也可以使用其他的提醒功能,如釘釘或飛書(shū)機(jī)器人提醒也是可以的,但郵箱報(bào)警功能的實(shí)現(xiàn)成本最低,所以本文我們就來(lái)看郵箱的報(bào)警提醒功能的具體實(shí)現(xiàn)2022-01-01java datetime數(shù)據(jù)類型去掉時(shí)分秒的案例詳解
在Java中,如果我們想要表示一個(gè)日期而不包括時(shí)間(時(shí)分秒),我們通常會(huì)使用java.time包中的LocalDate類,這篇文章主要介紹了java datetime數(shù)據(jù)類型去掉時(shí)分秒,需要的朋友可以參考下2024-06-06使用RestTemplate調(diào)用https接口跳過(guò)證書(shū)驗(yàn)證
這篇文章主要介紹了使用RestTemplate調(diào)用https接口跳過(guò)證書(shū)驗(yàn)證,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Spring boot整合Mybatis實(shí)現(xiàn)級(jí)聯(lián)一對(duì)多CRUD操作的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring boot整合Mybatis實(shí)現(xiàn)級(jí)聯(lián)一對(duì)多CRUD操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07java如何防止表單重復(fù)提交的注解@RepeatSubmit
@RepeatSubmit是一個(gè)自定義注解,用于防止表單重復(fù)提交,它通過(guò)AOP和攔截器模式實(shí)現(xiàn),結(jié)合了線程安全和分布式環(huán)境的考慮,注解參數(shù)包括interval(間隔時(shí)間)和message(提示信息),使用時(shí)需要注意并發(fā)處理、用戶體驗(yàn)、性能和安全性等方面,失效原因是多方面的2024-11-11java多線程并發(fā)中使用Lockers類將多線程共享資源鎖定
Lockers在多線程編程里面一個(gè)重要的概念是鎖定,如果一個(gè)資源是多個(gè)線程共享的,為了保證數(shù)據(jù)的完整性,在進(jìn)行事務(wù)性操作時(shí)需要將共享資源鎖定,這樣可以保證在做事務(wù)性操作時(shí)只有一個(gè)線程能對(duì)資源進(jìn)行操作,下面看一個(gè)示例2014-01-01springboot返回modelandview頁(yè)面的實(shí)例
這篇文章主要介紹了springboot返回modelandview頁(yè)面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10