1. Interrupt Interrupt method is used to break the thread. The state of the thread that called the method is set to the "break" state. Note: A thread break is simply a bit of the interrupt state of the thread and does not stop the thread. The user is required to monitor the status of the thread and do the processing. The method that supports thread interruption (that is, a method that throws interruptedexception after a thread break) is monitoring the interrupt state of a thread and throws an interrupt exception once the interrupt state of the thread is set to "break state".
2, Interrupted and isinterrupted
First look at the implementation of this method:
1 |
publicstatic booleaninterrupted () { |
2 |
returncurrentThread().isInterrupted(true); |
The method is to directly invoke the isinterrupted (true) method of the current thread. And then look at the implementation of isinterrupted:
1 |
publicbooleanisInterrupted () { |
2 |
returnisInterrupted( false); |
There are two main differences between the two methods:
- Interrupted is acting on the current thread, and isinterrupted is the thread that corresponds to the thread object that called the method. (threads that correspond to thread objects are not necessarily the threads that are currently running.) For example, we can invoke the Isinterrupted method of the B thread object in a thread. )
- Both methods will eventually invoke the same method, except that the argument is true and the other one is false;
The second difference is mainly in the parameters of the called method, let's take a look at what this parameter means. First look at the definition of the Called Method isinterrupted (Boolean arg):
1 |
privatenative boolean isInterrupted( booleanClearInterrupted); |
Originally this is a local method, can not see the source code. But it doesn't matter, we can tell by the parameter name that this parameter indicates whether to clear the status bit.
If this parameter is true, the status bit of the thread is returned and the original state bit is cleared (reverting to the original condition). This parameter is false, which returns the status bit of the thread directly.
These two methods are very well differentiated, only if the front-thread can clear its own interrupt bit (corresponding to the interrupted () method)
Does that make sense to you?
Thread.CurrentThread (). interrupt (); This clears the interrupt state so that the next time the Thread.interrupted () method is called, it returns to true because the interrupt flag has been restored.
The call to isinterrupted is simply a query break state and does not modify the state.
Interrupt () is used to set the interrupt state. Returns true to indicate that the interrupt state was set instead of being cleared. When we call this type of interruptible (throw interruptedexception) method such as sleep, wait and so on, once the method throws Interruptedexception, the interrupt state of the thread that is currently calling the method is automatically cleared by the JVM. That means we return FALSE when we call the thread's Isinterrupted method. If you want to keep the interrupt state, you can call the interrupt method again to set the interrupt state. The reason for this is that Java interrupts are not really interrupt threads, but only flag bits (interrupt bits) are set to notify the user. If you catch an interrupt exception, it means that the current thread has been interrupted and you do not need to keep the interrupt bit.
Interrupted is a static method that returns the interrupt state of the current thread. For example, if the current thread is interrupted (without throwing an interrupt exception, the interrupt state is cleared), and you call the interrupted method, the first time it returns true. The interrupt state of the current thread is then purged from the inside of the method. False is returned on the second call. If you have just started calling isinterrupted, it will always return true unless the interrupt state of the intermediate thread is cleared by other operations.
The differences between Java---Interrupt, interrupted, and isinterrupted