1. Result of thread interruption professional terminology
Isinterrupted interrupted interrupt
Tests if the front thread has been interrupted, and the interrupt state is canceled
Thread.interrupted ();
Adding an interrupt token to the current thread * * Does not really immediately stop the thread
Thread.Interrupt ();
Test whether the thread has been interrupted
thread.isinterrupted ();
2. Knowledge Points:
Calling interrupt directly does not interrupt the thread
package org.test;/* * Thread Interrupt Demo * * @author patzheng [email protected] * */public class yak {public static void main (String[] args) {Thread countThread = New thread (new countrunnable); Countthread.start (); Countthread.interrupt (); SYSTEM.ERR.PRINTLN ("Child threads are terminated by the main thread");}} class countrunnable implements runnable {private int maxvalue;public Countrunnable (Int maxvalue) {this.maxvalue = maxvalue;} @Overridepublic void run () {int sum = 0;for (int i = 0; i < maxvalue; i++) {sum += i; Long now = system.currenttimemillis ();while (System.currenttimemillis () - now < 200) {}}system.err.println (sum);}}
Terminate the thread in a different way: Ends the loop with the break statement, but the statement outside the loop executes. Using a
countthread. Interrupt (); You can see that the isinterupted returned by the thread after termination is true; if you switch to interupted, it will be output
False means that the terminating state of the thread is cleared
package org.test;import java.text.breakiterator;/* * Thread Interrupt Demo * * @ author patzheng [email protected] * */public class yak {public Static void main (String[] args) {thread countthread = new thread (new countrunnable (Ten)); Countthread.start (); Try {thread.sleep (2000);} catch (interruptedexception e) {e.printstacktrace ();} Countthread.interrupt (); SYSTEM.ERR.PRINTLN ("Child threads are terminated by the main thread");}} /** * again runnable inside directly with thread.currentthread can use some of the methods of thread * * @ Author patzheng [email protected]163.com * */class countrunnable implements runnable {private int maxvalue;public countrunnable (Int maxValue) { This.maxvalue = maxvalue;} @Overridepublic void run () {int sum = 0;for (int i = 0; i < maxvalue; i++) {if (! Thread.CurrentThread (). isinterrupted ()) {sum += i;} else {system.err.println (Thread.CurrentThread (). isinterrupted () + "status"); System.err.println (Thread.CurrentThread (). isinterrupted () + "status"); System.err.println ("break"); Long now = system.currenttimemillis ();while (System.currenttimemillis () - now < 1000) {}}system.err.println (sum);}}
This is then handled by an exception:
package org.test;/* * Thread Interrupt Demo * * @author patzheng [email Protected] * */public class yak {public static void main (String[] args) {thread countthread = new thread (new countrunnable (10)); Countthread.start (); Try {thread.sleep (2000);} catch (interruptedexception e) {e.printstacktrace ();} Countthread.interrupt (); SYSTEM.ERR.PRINTLN ("Child threads are terminated by the main thread");}} /** * again runnable inside directly with thread.currentthread can use some of the methods of thread * * @ author patzheng [email protected] * */class countrunnable implements Runnable {private int maxvalue;public countrunnable (Int maxvalue) {this.maxValue = maxvalue;} @Overridepublic void run () {try {int sum = 0;for (int i = 0; i < maxvalue; i++)   {if (! Thread.interrupted ()) {sum += i;} else {throw new interruptedexception (" thread interrupted");} Long now = system.currenttimemillis ();while (System.currenttimemillis () - now < 1000) {}}system.err.println (sum);} catch (exception e) {e.printstacktrace ();}}
What happens to the thread in the case of Sleep,wait,join interrupt back? Directly throws an exception, which is why you need to capture it when using these methods
Interruptedexception exception and the interrupt state is cleared
not all blocking methods are thrownInterruptedException. The input and output stream classes block waiting for I/O completion, but they do not throwInterruptedException, and will not return in advance if interrupted. However, for socket I/O, if a thread closes the socket, the blocking I/O operation on that socket ends prematurely and throws aSocketException. java.nionon-blocking I/O classes also do not support interruptible I/O, but can also be done by closing the channel or requestingSelectorto cancel the blocking operation. Similarly, try to get an internal lock operation (enter asynchronizedblock) cannot be interrupted, butReentrantLocksupport for interruptible capture mode.
Best practices for this exception the best approach is to interrupt the reset in the exception handling to facilitate the next processing
JAVA Thread Basics Summary-thread break