Thread的中斷


1,若是線程t經過Object的wait、或者線程的join 、sleep方法被阻塞, 那麼當你(在當前線程中)對t發送t.interrupt 消息時, t的中斷爲被清除,同時t將收到 InterruptedException 異常。html

2,若是t阻塞在InterruptibleChannel的I/O操做中, 這時對t 發送中斷消息會將t 的中斷爲置1, 並將收到 anClosedByInterruptException 異常, 同時 io管道關閉。java

3,若是線程t阻塞在 Selector 中, t收到中斷消息後立馬退出,不會收到異常。android


public void interrupt ()ide

Added in API level 1ui

Posts an interrupt request to this Thread. The behavior depends on the state of this Thread:this

  • Threads blocked in one of Object's wait() methods or one of Thread's join() or sleep() methods will be woken up, their interrupt status will be cleared, and they receive an InterruptedException.spa

  • Threads blocked in an I/O operation of an InterruptibleChannel will have their interrupt status set and receive anClosedByInterruptException. Also, the channel will be closed.線程

  • Threads blocked in a Selector will have their interrupt status set and return immediately. They don't receive an exception in this case.code



    See Alsohtm

  • interrupted()

  • isInterrupted()

相關文章
相關標籤/搜索