Stopping threads in multithreaded development is a very important technical point.
Stopping a thread in the Java language is not as straightforward as a break statement. Requires some technical processing.
first, the anomaly method
Use the exception method to stop a thread. First, we need to know how to use the two methods:
1. Interrupt () method
public class MyThread extends thread{@Overridepublic void Run () {for (int i = 1; I <= 10000; i++) {System.out.println (" I= "+i);}} public static void Main (string[] args) throws Exception {MyThread thread=new MyThread (); Thread.Start (); Thread.Sleep (10) ; Thread.Interrupt ();}}
The example above calls the interrupt () method to stop the thread, but the interrupt () method does not work as well as the For+break statement. You can stop the loop immediately.
Calling the interrupt () method does not actually stop when the current thread has a stopped tag. So suppose you stop the thread? Let's move on to the following view.
2. Infer if the thread is a stopped state
1), Interrupted ()
public class MyThread extends thread{@Overridepublic void Run () {for (int i = 1; I <= 10000; i++) {System.out.println (" I= "+i);}} public static void Main (string[] args) throws Exception {Try{mythread thread=new MyThread (); Thread.Start (); thread.sleep (+); Thread.Interrupt (); System.out.println ("Did the thread stop 1?") ---> "+thread.interrupted ()); System.out.println ("Did the thread stop 2?") ---> "+thread.interrupted ());} catch (Exception e) {e.printstacktrace ();} System.out.println ("End");}}
The result of printing from the console is that the thread is not stopped. This means that interrupted () tests whether the current thread is interrupted because the current thread is main. It has not been interrupted, so the result of printing is two false.how to cause the main thread to have an interrupt effect. Let's look at the following example:
public class Mythread{public static void Main (string[] args) {Thread.CurrentThread (). interrupt (); System.out.println ("Did the thread stop 1?") ----> "+thread.interrupted ()); System.out.println ("Did the thread stop 2?") ----> "+thread.interrupted ()); System.out.println ("End");}}
Judging from the results above. The interrupted () method does infer whether the current thread is a stopped state.
But why the 2nd value is false. Originally. the first time the method is called twice in a row clears the interrupt state. The second call therefore returns FLASE.
2), isinterrupted ()
public class MyThread extends thread{@Overridepublic void Run () {for (int i = 1; I <= 10000; i++) {System.out.println (" I= "+i);}} public static void Main (string[] args) {try{mythread thread=new MyThread (); Thread.Start (); Thread.Sleep (10); Thread.Interrupt (); System.out.println ("Did the thread stop 1?") ---> "+thread.isinterrupted ()); System.out.println ("Did the thread stop 2?") ---> "+thread.isinterrupted ());} catch (Exception e) {e.printstacktrace ();} System.out.println ("End");}}
The result shows that the method isinterrupted () is not cleared, so two true is printed.
3. Stop Thread
public class MyThread extends thread{@Overridepublic void Run () {for (int i = 1; I <= 10000; i++) {if (this.interrupted ( ) {System.out.println ("Thread is stopped, I'm quitting."); break;} System.out.println ("i=" +i); SYSTEM.OUT.PRINTLN ("Assuming this is still a loop, then I will continue to run." Thread does not stop ");} public static void Main (string[] args) {try{mythread thread=new MyThread (); Thread.Start (); Thread.Sleep (10); Thread.Interrupt (); System.out.println ("End");} catch (Exception e) {e.printstacktrace ();}}}
Assuming this is the case, the thread does not stop. Now we're changing the code. The so-called
exception method stops the thread.
public class MyThread extends thread{@Overridepublic void Run () {try{for (int i = 1; I <= 10000; i++) {if (This.interrup Ted ()) {System.out.println ("Thread is stopped, I'm quitting."); throw new Interruptedexception ();} System.out.println ("i=" +i); System.out.println ("Have I been running?" ");} catch (Interruptedexception e) {System.out.println ("---this thread has stopped---"); E.printstacktrace ();}} public static void Main (string[] args) {try{mythread thread=new MyThread (); Thread.Start (); Thread.Sleep (10); Thread.Interrupt (); System.out.println ("End");} catch (Exception e) {e.printstacktrace ();}}}
second, stop in the sleepWhat happens if a thread stops a thread in the sleep () state?
public class MyThread extends thread{@Overridepublic void Run () {try{system.out.println ("run Start"); Thread.Sleep (1000000); System.out.println ("Run End");} catch (Interruptedexception e) {System.out.println ("Sleep is stopped, state:--->" +this.isinterrupted ()); E.printstacktrace ( );}} public static void Main (string[] args) {try{mythread thread=new MyThread (); Thread.Start (); Thread.Interrupt (); Thread.Sleep (1000);} catch (Exception e) {System.out.println ("main catch"); E.printstacktrace ();}}}
from the results we can see that when the thread sleeps, stopping a thread will be abnormal. and clears the stop state. We are in front of the abnormal stop thread, are sleep first, in the stop thread, and the opposite of the operation, I need to note when writing code.
public class MyThread extends thread{@Overridepublic void Run () {try{for (int i = 1; I <= 10000; i++) {System.out.print ln ("i=" +i);} System.out.println ("Run Start"); Thread.Sleep (1000000); System.out.println ("Run End");} catch (Interruptedexception e) {System.out.println ("The thread is stopped. In sleep, Status:---> "+this.isinterrupted ()); E.printstacktrace ();}} public static void Main (string[] args) {try{mythread thread=new MyThread (); Thread.Start (); Thread.Interrupt ();} catch (Exception e) {System.out.println ("main catch"); E.printstacktrace ();}}}
iii. cessation of violence
public class MyThread extends thread{private int i=0; @Overridepublic void Run () {Try{while (true) {i++; System.out.println ("i=" +i); Thread.Sleep (1000);}} catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {try{mythread thread=new MyThread (); Thread.Start (); Thread.Sleep (6000); Thread.stop ();} catch (Exception e) {e.printstacktrace ();}}}
the Stop () method has been deprecated, assuming that the thread is forced to stop. There will be some cleanup work that has not been completed. There is a lock on the object to understand the lock, resulting in data synchronization phenomenon, so the development time prohibit the use of this method to violence to stop the thread.
Iv. using return to stop the thread
public class MyThread extends thread{private int i=0; @Overridepublic void Run () {Try{while (true) {i++;if (this.interrupte D ()) {System.out.println ("thread stopped"); return;} System.out.println ("i=" +i);} catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {try{mythread thread=new MyThread (); Thread.Start (); Thread.Sleep (+); Thread.Interrupt ();} catch (Exception e) {e.printstacktrace ();}}}
PS: It is only recommended to use the exception method to stop the thread, because the exception can be thrown up in the catch block, so that the thread stop event is propagated.
Java multi-thread stop thread