Java multithreaded interrupt code
1、 There are three main methods to terminate threads in Java:
① The thread exits normally, that is, the execution of the run () method is completed
② Use the stop() method in the thread class (expired, not recommended) to forcibly terminate the thread.
③ Use interrupt mechanism
t. When stop() is called, terminating the thread will cause the lock held by the thread to be forcibly released and held by other threads. Therefore, it may cause inconsistency with the expected result. The following interrupt semaphores are used to interrupt threads in a non blocking state:
2、 Java thread interrupt mechanism
Let's take a look at the three methods in the thread class:
1. Public static Boolean interrupted(): check whether the current thread has been interrupted. The interrupt state of the thread is cleared by this method. If the method is called twice in a row, the second call will return false (except that the current thread interrupts again after the first call has cleared its interrupt state and before the second call has verified the interrupt state).
2. Public Boolean isinterrupted(): test whether the thread has been interrupted. The interrupt state of the thread is not affected by this method.
3. Public void interrupt(): interrupt the thread.
Interrupt () just changes the interrupt state Interrupt () does not terminate a running thread.
Normal output:
In fact, when the interrupt () method is called, only the interrupt state of the thread to be interrupted is set. At this time, the interrupted thread can be interrupted through isinterrupted () or thread The interrupted () method determines whether the interrupt status of the current thread is marked as interrupt.
Calling the thread's wait(), wait (long) or wait (long, int) will make it enter the waiting (blocking) state, or calling the thread's join(), join (long), join (long, int), sleep (long), sleep (long, int) will also make it enter the blocking state. If a thread calls its interrupt () method when it is in blocking state, its "interrupt state" will be cleared and an interruptedexception exception will be received. For example, a thread enters a blocking state through wait(), and interrupts the thread through interrupt(); Calling interrupt() will immediately set the interrupt flag of the thread to "true", but since the thread is blocked, the "interrupt flag" will be cleared to "false" immediately, and an exception of interruptedexception will be generated.
Normal operation results:
System reset. We can manually use thread Interrupted() resets the interrupt state of the current thread (that is, clears the interrupt state). In fact, it continuously checks the interrupt state value in sleep, wait and join methods, and throws its own interruptedexception.
Interrupt: if a thread is blocked in calling the wait (), wait (long) or wait (long, int) methods of object class, or the join (), join (long, int), sleep (long) or sleep (long, int) methods of this class, its interrupt state will be cleared and it will receive an interruptedexception.
If the thread is blocked in an I / O operation on an interruptible channel (Java. NiO. Channels. Interruptible channel), the channel will be closed, the interrupt state of the thread will be set, and the thread will receive a closedbyinteruptexception.
If the thread is blocked in a selector (Java. NiO. Channels. Selector), the interrupt state of the thread will be set, and it will immediately return from the selection operation, possibly with a non-zero value, as if the wakeup method of the selector was called.
If the previous conditions are not saved, the interrupt state of the thread will be set.
There is no need to interrupt an inactive thread.
Detect interrupts: how to detect interrupts depends on what the thread does.
If a thread calls a method that can throw an interruptexception, the interruptexception is captured and processed in the catch block (usually exiting the run method to interrupt the thread)
If you call other methods, you can check the thread when it is idle Interrupted to judge whether the interrupt signal is received. Process after confirming that the interrupt signal is received. You can throw an interruptexception to be consistent with the previous processing method
Interrupt state: the interrupt mechanism of a thread is implemented using the internal flag of interrupt state. The interrupt state is set when calling the interrupt () method of the thread (refer to the interrupt method description above).
It can be found that isinterrupted is declared as a native method, depending on the underlying implementation of the JVM. Calling the interrupt method of the thread does not immediately trigger an interrupt, but sets the interrupt flag inside the JVM. Therefore, by checking the interrupt flag, the application can do some special operations or ignore the interrupt completely.
Actually thread The interrupt () method actually notifies the thread in some way and does not directly abort the thread. It's up to the person who writes the code to decide what to do. Usually, we abort the thread.
3、 Some threads that do not throw interruptedexception block operations
For some thread blocking operations, the JVM does not automatically throw an interruptedexception exception. For example, some I / O operations and internal lock operations. For such operations, interrupts can be simulated in other ways:
1)java. Asynchronous socket I / O in io
When reading and writing sockets, the read and write methods of InputStream and OutputStream will block waiting, but will not respond to Java interrupts. However, after calling the close method of socket, the blocked thread will throw a socketexception exception.
2) Asynchronous I / O using selector
If the thread is blocked in the selector Select (in Java. NiO. Channels). Calling the wakeup method will cause a closedselectorexception exception.
3) Lock acquisition
If the thread is waiting to acquire an internal lock, we will not be able to interrupt it. However, using the lockinterruptible method of lock class, we can provide interrupt capability while waiting for the lock.
summary
The above is all about the detailed explanation of Java multithreaded interrupt code in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java multithreaded callback method example analysis, Java multithreaded programming example, talking about the advantages and code examples of Java multithreading, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!