1:code
public class ThreadTest1 extends thread{@Overridepublic void Run () {//TODO auto-generated method Stubsuper.run (); for (int i=0;i<500000;i++) {if (this.interrupted ()) {System.out.println ("exited---"); System.out.println ("--" +i);}}} public static void Main (string[] args) {ThreadTest1st=new ThreadTest1 (); Th.start (); try {thread.sleep (2000); Th.interrupt ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} LOG:--331653--331654--331655--331656--331657--331658 has exited---
2:code
and 1 contrast
public class ThreadTest1 extends thread{@Overridepublic void Run () {//TODO auto-generated method Stubsuper.run (); for (int i=0;i<500000;i++) {if (this.interrupted ()) {System.out.println ("exited---"); System.out.println ("--" +i);} System.out.println ("If the output is diverted-indicates that the thread did not stop!") ");}} public static void Main (string[] args) {ThreadTest1st=new ThreadTest1 (); Th.start (); try {thread.sleep (2000); Th.interrupt ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} --332195--332196--332197--332198--332199 has exited---if the output is diverted-the description thread does not stop! Says the thread is still gone for the statement after the for.
3: Exception throw method exit thread
/ Exception throw method exit thread ********/public class ThreadTest1 extends thread {@Overridepublic void run () {//TODO auto-generated Metho D Stubsuper.run (); try {for (int i = 0; i < 500000; i++) {if (this.interrupted ()) {System.out.println ("exited---"); throw n EW interruptedexception ();} System.out.println ("--" + i);} System.out.println ("If the output is diverted-indicates that the thread did not stop!") ");} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} public static void Main (string[] args) {ThreadTest1st=new ThreadTest1 (); Th.start (); try {thread.sleep (2000); Th.interrupt ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} LOG:--341903--341904--341905--341906--341907--341908--341909--341910 has exited---java.lang.InterruptedExceptionat Guoxw. Threadtest1.run (threadtest1.java:13)
4: Quit thread in hibernation
Stop thread in hibernation; public class ThreadTest2 extends thread {@Overridepublic void run () {//TODO auto-generated method Stubsuper . run (); try {System.out.println ("beagin--"); Thread.Sleep (20000); System.out.println ("end--");} catch (Interruptedexception e) {//TODO auto-generated catch BlockSystem.out.println ("Enter catch-" +this.isinterrupted ()) ; E.printstacktrace ();}}} public static void Main (string[] args) {ThreadTest2nd=new ThreadTest2 (); Th.start (); try {th.interrupt ();} catch ( Exception e) {e.printstacktrace ();}} beagin--into Catch-falsejava.lang.interruptedexception:sleep interruptedat java.lang.Thread.sleep (Native Method) at Guoxw. Threadtest2.run (THREADTEST2.JAVA:10)
5:stop Violent Exit thread
Stop violence stop public class ThreadTest3 extends thread{@Overridepublic void Run () {//TODO auto-generated method Stubsuper.run () ; while (true) {System.out.println ("Output information--"); try {thread.sleep (+);} catch (Interruptedexception e) {//TODO Auto-generated catch Blocke.printstacktrace (); System.out.println ("Enter Catch");}}}} public static void Main (string[] args) {ThreadTest3rd=new ThreadTest3 (); Th.start (); try {thread.sleep (6000); Th.stop ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} LOG: Output information-output information-output information-output information--note: Using the stop release lock will result in inconsistencies in the data.
6: Use return to stop the thread
public class ThreadTest4 extends thread{@Overridepublic void Run () {//TODO auto-generated method Stubsuper.run (); while (true) {if (this.isinterrupted ()) {System.out.println ("stopped----"); return;} System.out.println ("Do you want to continue?") ");}}} public static void Main (string[] args) {ThreadTest4th=new ThreadTest4 (); Th.start (); try {thread.sleep (6000); Th.interrupt ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Did you continue with the execution? Did you continue with the execution? Did you continue with the execution? Did you continue with the execution? Stopped----.
Stop exit operation for a thread