Interrupt () Method: The function is a thread break.
- This thread interrupt itself is allowed, and the interrupt token is set to True
- when other threads invoke the interrupt () method of this thread, the permission is checked through CheckAccess (). This may throw a SecurityException exception.
- if the thread is in a blocking state, its interrupt () method is called, then its "break state" is cleared and a Interruptedexception exception is received.
- For example, a thread enters a blocking state through wait (), at which point the thread is interrupted by interrupt (), and the call interrupt () immediately sets the thread's interrupt token to "true", but the interrupt token is immediately cleared to "false" because the thread is in a blocked state. , at the same time, produces an interruptedexception exception.
- If the thread is blocked in a selector selector, it is interrupted by interrupt (), the thread's interrupt token is set to true, and it is immediately returned from the selection operation.
- If this is not the case, the interrupt token is set to "true" when the thread is broken through interrupt ().
Interrupted () method
Determines whether the current thread is in a broken state. is a static method of the class and clears the interrupt state of the thread.
1 Public Static Boolean interrupted () {2return CurrentThread (). isinterrupted (true); 3 }
Isinterrupted () method
Determine if the calling thread is in a broken state
For example:
Public Static void New// defines a thread, pseudo code does not specifically implement thread.isinterrupted (); // determine if thread is in a broken state, not the main thread is in a broken state thread.isinterrupted ();//Determine if the main thread is in a broken state}
Thread stop
- Terminates the thread with the break token.
@Override Public void run () { while (! Isinterrupted ()) {// perform task ... }}
Description: Isinterrupted () is a thread that determines if the interrupt token is true. When the thread is in a running state and we need to terminate it, the interrupt () method of the thread can be called, and the interrupt token with the thread is true, that is, isinterrupted () returns True. At this point, you exit the while loop.
Note: interrupt () does not terminate a thread that is in a "Running state"! It sets the interrupt token for the thread to true.
Private volatile Boolean true ; protected void false; } @Override publicvoid run () { while (flag) { // perform Tasks ... }}
Description: There is a flag tag in the thread, its default value is true, and we provide stoptask () to set the flag flag. When we need to terminate the thread, invoking the thread's Stoptask () method allows the thread to exit the while loop.
Note: The flag is defined as a volatile type to guarantee the visibility of the flag. This thread can see the value of the modified flag after other threads have modified flag through Stoptask ().
Synthetic threads are in the "blocking state" and "Run State" termination mode, and the common termination thread is compared in the following form:
@Override Public void run () { try { // 1. isinterrupted () guarantees that the thread is terminated as long as the interrupt is marked as true. while (! Isinterrupted ()) { // perform task ... catch (Interruptedexception IE) { // 2. The interruptedexception exception guarantees that the thread is terminated when the interruptedexception exception is generated. }}
Java multithreading interrupt (), Interrupted (), isinterrupted () method differences