There are 3 ways to terminate a running thread in Java:
1, use the exit flag, is the thread normal exit, that is, the Run method completes the thread termination.
2, using the Stop method to force the termination of the thread, eggs are not recommended to use this method, because stop, suspend and resume, are obsolete methods.
3. Using the interrupt method, most stop a thread using the Thread.Interrupt () method, but this method does not terminate a running thread, but also needs to add some judgment to complete the thread stop.
Let me show you a few examples of the effects of stopping threads.
One, unable to stop the thread--interrupt
This example calls the interrupt () method to stop the thread, but the effect is not ideal, and the running thread cannot be stopped because the call to interrupt () method simply hits a stop mark in the current thread and does not really stop the thread.
public static void Main (string[] args) {
MyThread1 t = new MyThread1 (); T.start (); try {thread.sleep (2);
thread.interrupted ();} catch (Interruptedexception e) {System.out.println ("main catch"); E.printstacktrace ();}} public static class MyThread1 extends Thread {@Overridepublic void run () {
Super.run (); for (int i = 0; i < i++) {System.out.println ("i=" + (i + 1)}}}
running Results I intercept the last paragraph, the console just output to 5000, indicating that the call interrupt method does not stop the thread, the result is as follows:
So how do you stop a thread? Here I come back to introduce.
Second, can stop the thread--anomaly method
Let's look at an example:
public static void Main (string[] args) throws interruptedexception {MyThread t = new MyThread (); T.start (); Thread.Sleep (50); t.interrupt (); System.out.println ("End");} public static class MyThread extends Thread {@Overridepublic void Run () {//TODO auto-generated method Stubsuper.run (); (int i = 0; i < i++) {if (this.interrupted ()) {System.out.println ("is already stopped!) I'm quitting! "); break;} System.out.println ("i=" + (i + 1));} System.out.println ("If I am output, the Run method continues to execute and the thread does not stop!") ");}}
The output results are as follows:
from the results above, it's just the end of the for loop, but then the output " If I was output, the Run method continues to execute and the thread does not stop! This sentence means that it is not possible for the Run method to stop, so the write is not possible. Let's look at an example below.
public static void Main (string[] args) throws interruptedexception {MyThread t = new MyThread (); T.start (); Thread.Sleep (50); t.interrupt (); System.out.println ("End");} public static class MyThread extends Thread {@Overridepublic void run () {super.run (), try {for (int i = 0; i <; i++ {if (this.interrupted ()) {System.out.println ("is already stopped!) I'm quitting! ");
Exception method End thread throw new Interruptedexception ();} System.out.println ("i=" + (i + 1));} System.out.println ("I'm under for");} catch (Interruptedexception e) {System.out.println ("Enter catch! in the thread thread's Run method "); E.printstacktrace ();}}}
Here's a look at the output on the console:
to analyze the results, throw new Interruptedexception () This sentence is no longer executed, because " I am under for " This sentence did not lose, but into the catch, the end of the entire run method. So the exception method to stop the threading method is reliable.
Third, stop the thread in the sleep
public static void Main (string[] args) {try {MyThread t = new MyThread (); T.start (); Thread.Sleep (200); t.interrupt ();} catch (Exception e) {System.out.println ("main catch"); E.printstacktrace ();} System.out.println ("end!");} public static class MyThread extends Thread {@Overridepublic void run () {super.run (); try {System.out.println ("Run Begin") ;
In order to ensure that the thread can be stopped, the sleep time is as long as possible, if the time is too short, it is possible that the main thread has not been dormant, the child thread sleep is over, the thread is running normally. sleep (+); System.out.println ("Run End");} catch (Exception e) {System.out.println ("be stopped in slumber!") Get in catch!. "+ this.isinterrupted ()); E.printstacktrace ();}}}
The results of the operation are as follows:
to analyze the result, "run end" This sentence does not have the output, explained that the son line thread's sleep below the content no longer executes, but enters the catch inside, thus stopped the Run method. The explanation is also reliable.
Four, can stop the thread--violence stop
using the Stop () method to stop a thread is a very violent behavior, and using the Stop () method results in a different running result that has been deprecated by the official document. Let me show you an example.
public static void Main (string[] args) {MyThread t = new MyThread (); T.start (); try {
Thread.Sleep (9000); T.stop ();} catch (Exception e) {e.printstacktrace ();}} public static class MyThread extends Thread {private int i = 0, @Overridepublic void Run () {super.run (); while (true) {I++;s Ystem.out.println ("i=" + i); try {sleep (+);} catch (Interruptedexception e) {e.printstacktrace ()}}}}
V. Using return to stop a thread
combining interrupt () with return also enables the effect of stopping a thread. However, we recommend that you use a throw exception to stop the thread, because you can handle information about when an exception occurs. Let's look at a piece of code.
public static void Main (string[] args) throws interruptedexception {MyThread t = new MyThread (); T.start (); Thread.Sleep (2); T.interrupt ();} public static class MyThread extends Thread {@Overridepublic void run () {Super.run (), for (int i = 0; i <; i++) {if (This.interrupted ()) {System.out.println ("It's already stopped! I'm quitting! "); return;} System.out.println ("i=" + (i + 1));}}}
let's look at the results of the operation:
Vi. Suspending Threads
Pause thread Odor This thread can also resume running, suspend the thread using the Suspend () method in Java, and resume the thread using the resume () method. Let's look at a piece of code.
public static void Main (string[] args) throws interruptedexception {MyThread t = new MyThread (); T.start ();//a segment pauses thread t.suspend (); System.out.println ("a=" +system.currenttimemillis () + "\ T" + "i=" +t.geti ()); Thread.Sleep (500); System.out.println ("a=" +system.currenttimemillis () + "\ T" + "i=" +t.geti ()),//b segment resumed t.resume (); System.out.println ("b=" +system.currenttimemillis () + "\ T" + "i=" +t.geti ()); Thread.Sleep (500); System.out.println ("b=" +system.currenttimemillis () + "\ T" + "i=" +t.geti ()),//c segment suspend thread t.suspend (); System.out.println ("c=" +system.currenttimemillis () + "\ T" + "i=" +t.geti ()); Thread.Sleep (500); System.out.println ("c=" +system.currenttimemillis () + "\ T" + "i=" +t.geti ());} public static class MyThread extends Thread {private long i = 0;public long Geti () {return i;} public void SetI (long i) {this.i=i;} @Overridepublic void Run () {super.run (); while (true) {i++;}}}
The following analysis of the results of the operation: in A, the first execution of the suspend () suspend the thread method, so from the results can be seen, the current time of the system is changing, but I value is always 0, indicating that the thread is not executing, is still the initialized value, to the B segment, the resume () recovery thread method , from the results can be seen, the current system time is changing, I value is also changed, said the surface thread has been restored and executed, to the C segment, the result is the same as the cause of paragraph A.
I have said a lot of small examples, if you personally knocked over the code, look at the execution results, there will be a better understanding, after all, the results of thread execution is not a single.
Java Thread Stop Threads