Java線程中的interrupt方法解讀
interrupt方法
首先梳理Thread關于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實例對象調用方法interrupt,有這幾種情況
? 1.在線程里面有wait,sleep,join方法,會彈出InterruptedException,狀態(tài)會被清除
? 2.io里面,會彈出異常,狀態(tài)會被設置,…
? 3.如果線程阻塞在nio中的Selector中,狀態(tài)會被設置,…
? 4.如果不是上面,這些情況,狀態(tài)會被設置
可以看出interrupt是關于狀態(tà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();
}
線程會在(1000+)ms后結束,整個程序也會結束
第二種,有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();
}
方法并不會退出,只是打出了一個InterruptException
如何處理中斷?
- 上文都在介紹如何獲取中斷狀態(tài),那么當我們捕獲到中斷狀態(tài)后,究竟如何處理呢?
- Java類庫中提供的一些可能會發(fā)生阻塞的方法都會拋InterruptedException異常,如:BlockingQueue#put、BlockingQueue#take、Object#wait、Thread#sleep。
- 當你在某一條線程中調用這些方法時,這個方法可能會被阻塞很長時間,你可以在別的線程中調用當前線程對象的interrupt方法觸發(fā)這些函數(shù)拋出InterruptedException異常。
- 當一個函數(shù)拋出InterruptedException異常時,表示這個方法阻塞的時間太久了,別人不想等它執(zhí)行結束了。
- 當你的捕獲到一個InterruptedException異常后,亦可以處理它,或者向上拋出。
- 拋出時要注意???:當你捕獲到InterruptedException異常后,當前線程的中斷狀態(tài)已經被修改為false(表示線程未被中斷);此時你若能夠處理中斷,則不用理會該值;但如果你繼續(xù)向上拋InterruptedException異常,你需要再次調用interrupt方法,將當前線程的中斷狀態(tài)設為true。
- 注意:絕對不能“吞掉中斷”!即捕獲了InterruptedException而不作任何處理。這樣違背了中斷機制的規(guī)則,別人想讓你線程中斷,然而你自己不處理,也不將中斷請求告訴調用者,調用者一直以為沒有中斷請求。
更新相關interrupt方法的意義
public void interrupt()
Thread里面的打斷函數(shù),可以使用新建一個Thread對象,執(zhí)行thread.interrupt();
修改調用線程的interrupt 的status
public static boolean interrupted()
該方法可以判斷是否被打斷,但是這個interrupt的status會被清除,
/**
* 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()
該方法可以判斷是否被打斷
到此這篇關于Java線程中的interrupt方法解讀的文章就介紹到這了,更多相關interrupt方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java8?Stream大數(shù)據量List分批處理切割方式
這篇文章主要介紹了java8?Stream大數(shù)據量List分批處理切割方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Java語言實現(xiàn)簡單FTP軟件 輔助功能模塊FTP站點管理實現(xiàn)(12)
這篇文章主要為大家詳細介紹了Java語言實現(xiàn)簡單FTP軟,輔助功能模塊FTP站點管理的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
擲6面骰子6000次每個點數(shù)出現(xiàn)的概率
今天小編就為大家分享一篇關于擲6面骰子6000次每個點數(shù)出現(xiàn)的概率,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解
這篇文章主要介紹了SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09

