interrupt method
Interrupt literally means interrupt, but in Java the Thread.interrupt() method actually notifies the thread in some way and does not directly terminate the thread. It's up to the person who wrote the code to decide what to do, and usually we abort the thread.
If the thread is calling the wait(), wait(long) or wait(long, int) method of the Object class, or the join(), join(long), join(long, int), sleep(long) or sleep of the class (long, int) is blocked during the method, its interrupt status will be cleared, and it will also receive an InterruptedException.
If the thread blocks in an I/O operation on an interruptible channel (java.nio.channels.InterruptibleChannel), the channel will be closed, the thread's interrupt status will be set and the thread will receive a ClosedByInterruptException.
If the thread is blocked in a Selector (java.nio.channels.Selector), the thread's interrupt status will be set and it will return immediately from the select operation, possibly with a non-zero value, as if select was called The wakeup method of the device is the same.
If none of the previous conditions have been saved, the thread's interrupt status will be set.
Interrupting an inactive thread has no effect.
detect interrupt
How the interrupt is detected depends on what the thread is doing.
If the thread calls a method that can throw InterruptException, catch the InterruptException and then handle it in the catch block (usually exiting the run method to interrupt the thread)
If you call other methods, you can check Thread.interrupted when idle to determine whether an interrupt signal is received, and process it after confirming that the interrupt signal is received. An InterruptException can be thrown to be consistent with the previous handling method
interrupt status
The thread's interrupt mechanism is implemented using the internal flag of the interrupt status. The interrupt status is set when the thread's interrupt() method is called (see the interrupt method description above).
There are two ways to obtain the interrupt status of a thread:
Call the static method Thread.interrupted(). In addition to returning the interrupt status of the current thread, this method also clears the interrupt status of the current thread. In other words, if this method is called twice in a row, the second call will return false (after the first call has cleared its interruption status, and before the second call has verified the interruption status, the current thread is interrupted again. exceptions).
Call the isInterrupted() method of the specified thread. This method only returns the interrupt status of the specified thread without affecting the interrupt status of the thread.
There are two ways to clear the interrupt status of a thread:
As mentioned above, calling Thread.interrupted()
When the interrupt() method of the thread is called to cause InterruptedException, the interrupt status of the thread has been cleared at the same time, including the wait(), wait(long) or wait(long, int) method of the Object class, or the thread's join() , join(long) , join(long, int) , sleep(long) or sleep(long, int) method