Java 線程中斷機(jī)制整理

前言

書寫約定

  1. Object代表 Object
  2. object代表具體的 Object對象
  3. Thread代表 Thread
  4. thread代表具體的 Thread·對象

操作類型

JDK文檔里Thread類中關(guān)于interrupt的方法有三種

  1. public void interrupt()
Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or 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.

If this thread is blocked in an I/O operation upon an InterruptibleChannel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a 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 wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.
  1. public static boolean interrupted()
Tests whether the current thread has been interrupted. The interrupted status 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).

A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.
  1. public boolean isInterrupted()
Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.

A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.

其中,public void interrupt()用于中斷線程,可以外部中斷,也可以自身中斷

# 外部中斷
thread.interrupt();
# 自身中斷
Thread.currentThread.interrupt();

public static boolean interrupted()public boolean isInterrupted()用于檢測線程中斷狀態(tài),區(qū)別在于

  • public static boolean interrupted()會清除thread的中斷標(biāo)志狀態(tài)
  • public boolean isInterrupted()不會清除thread的中斷標(biāo)志狀態(tài)

中斷處理

線程的中斷標(biāo)志位被設(shè)置為true,不代表線程會立刻終止,具體行為應(yīng)采用檢測Thread.currentThread().isInterrupted()的方式,手動編碼中斷邏輯。

中斷vsBlocked

線程在Blocked狀態(tài)時,會檢測中斷標(biāo)志位,如果為true,則會清除中斷標(biāo)志位拋出相應(yīng)異常,如圖

總結(jié)

線程處于Blocked狀態(tài)并檢測到中斷,會清除中斷標(biāo)志并接收相應(yīng)異常(再拋出),編碼實(shí)踐如下:

while (!Thread.currentThread().isInterrupted()) {
  // do someting
}

// or
while (!Thread.currentThread().isInterrupted()) {
  try {
    // do someting
    Thread.sleep(1000);
  } catch (InterruptedException e) {
    // log
    Thread.currentThread.interrupt();
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容