The difference between thread interrupt related methods interrupt, interrupted and isInterrupted

interrupt

Interrupt this thread. The state of the thread that calls this method will be set to the "interrupted" state, and the thread will not stop running.
If the thread is blocked when calling the wait(), wait(long) or wait(long, int) method or join(), join(long), join(long, int) method of the Object class, sleep(long) or For methods such as sleep(long, int), the interrupted status will be cleared, and InterruptedException will be received. ( Before the JUC lock or queue-related class throws InterruptedException, the following static method interrupted will be called to clear the interrupted state. So this is why it is necessary to throw this exception again or call the interrupt again, otherwise the thread interruption will be lost Status. For example: the main thread monitors the interrupt status of the child thread to make different treatments. If the child thread throws this exception and does not handle it correctly, the main thread will not perceive the interrupt status of the child thread. )
If this thread is in the I/O operation of InterruptibleChannel If blocked, the channel will be closed, the interrupt status of the thread will be set, and the thread will receive java.nio.channels.ClosedByInterruptException.
If this thread is blocked in java.nio.channels.Selector, the interrupt status of this thread will be set, and it will immediately return from the selection operation (may have a non-zero value), just like calling the wakeup method of the selector.
If none of the above conditions are true, the interrupt status of the thread will be set.
Interrupting a thread that is not running has no effect 

interrupted

Static method. Test whether the current thread has been interrupted. Through this method, the interruption status of the thread can be cleared. In other words, if you want to call this method twice in a row, the second call will return false (unless the current thread is interrupted again after the first call clears its interrupted status and before the second call checks its status).
Since this method returns false, it will reflect the thread interruption and be ignored because the thread was not active when it was interrupted

  public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }

isInterrupted

Test whether this thread has been interrupted. The interruption status of the thread is not affected by this method.
Since this method returns false, it will reflect the thread interruption and be ignored because the thread was not active when it was interrupted

   public boolean isInterrupted() {
        return isInterrupted(false);
    }

isInterrupted(boolean)

    private native boolean isInterrupted(boolean ClearInterrupted);

 It can be seen that both interrupted and isInterrupted call the internal local method isInterrupted, and the parameter is whether the interrupt status is clear.

The difference between interrupted and isInterrupted : interrupted acts on the current thread, and isInterrupted acts on the thread object that calls this method. For example: call the isInterrupted method of the child thread sub in the main thread main, and call the static interrupted method at the same time. Then isInterrupted acts on the child thread sub, and interrupted acts on the main thread main

Guess you like

Origin blog.csdn.net/sinat_33472737/article/details/114681170