美文网首页
Java 线程中断机制整理

Java 线程中断机制整理

作者: 一杉风雨 | 来源:发表于2019-05-24 11:42 被阅读0次

    前言

    书写约定

    1. Object代表 Object
    2. object代表具体的 Object对象
    3. Thread代表 Thread
    4. thread代表具体的 Thread·对象

    操作类型

    JDK文档里Thread类中关于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()用于检测线程中断状态,区别在于

    • public static boolean interrupted()会清除thread的中断标志状态
    • public boolean isInterrupted()不会清除thread的中断标志状态

    中断处理

    线程的中断标志位被设置为true,不代表线程会立刻终止,具体行为应采用检测Thread.currentThread().isInterrupted()的方式,手动编码中断逻辑。

    中断vsBlocked

    线程在Blocked状态时,会检测中断标志位,如果为true,则会清除中断标志位抛出相应异常,如图

    总结

    线程处于Blocked状态并检测到中断,会清除中断标志并接收相应异常(再抛出),编码实践如下:

    while (!Thread.currentThread().isInterrupted()) {
      // do someting
    }
    
    // or
    while (!Thread.currentThread().isInterrupted()) {
      try {
        // do someting
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        // log
        Thread.currentThread.interrupt();
      }
    }
    

    相关文章

      网友评论

          本文标题:Java 线程中断机制整理

          本文链接:https://www.haomeiwen.com/subject/ssnwzqtx.html