After an External Thread calls the thread. Interrupt () method to a thread, the Java language processing mechanism is as follows:
If the thread is in a resumable state. wait (), or selector. select (), thread. sleep () and other specific APIs that will be blocked), the thread will be immediately awakened and will be subject to an interruptedexception. At the same time, if the thread is blocked on Io, the corresponding resource is disabled. If the thread does not execute the "thread. interrupted () method (not interrupt) Next, the thread will shut down these resources when it processes Any Io resources. Of course, the solution is to call interrupted (), but the programmer needs to set it based on the Code logic and determine whether the interrupt can be directly ignored based on their own needs, or Exit immediately.
If the thread is in an uninterrupted state, that is, the above API is not called, Java only sets the thread's interrupt state, and nothing else will happen, if the thread then calls the number of lines to block the API, the thread will immediately jump out and throw interruptedexception. The next thing will be the same as the first one. If you do not call the blocking API, this thread will continue to run. Unless you want to implement such a thread, generally high-performance code will certainly have functions such as wait () and yield () that sell CPU, without the latter situation. Source: bytes) the thread has a function similar to sleep to transfer the CPU. In this way, when the sleep process is interrupted by other threads, interruptedexception will be thrown. In this way, we can handle the catch. For example, continue or break. 2) if there is no function similar to sleep, you can use a while-like check, often check thread. whether interrupted () returns true. If yes, you can make your own predefined logical tasks, such as continue or break. If neither of the preceding methods is available, the thread will run all the time and the returned status of thread. interrupted () can be changed. The thread cannot be stopped.