1. Anomaly method
Public classMyThreadextendsThread {@Override Public voidrun () {Super. Run (); Try { for(inti = 0; i < 5000000; i++) { if( This. Interrupted ()) {System.out.println ("I'm going to stop ... "); Throw Newinterruptedexception (); \ \ Throws an exception } System.out.println ("I=" + (i+1)); } System.out.println ("I'm underneath for the. "); } Catch(interruptedexception e) {System.out.println ("In MyThread catch ..."); E.printstacktrace (); } } Public Static voidMain (string[] args) {Try{MyThread MyThread=NewMyThread (); Mythread.start (); Thread.Sleep (2000); Mythread.interrupt (); } Catch(interruptedexception e) {System.out.println ("Main Catch"); E.printstacktrace (); } System.out.println ("End"); }}
Results:
2, stop in the sleep, that is, in the sleep () state.
Public classMyThreadextendsThread {@Override Public voidrun () {Super. Run (); Try{System.out.println ("Run Begin"); Thread.Sleep (200000); System.out.println ("Run End"); } Catch(interruptedexception e) {System.out.println ("To be stopped in slumber!" entered catch!. + This. isinterrupted ()); E.printstacktrace (); } } Public Static voidMain (string[] args) {Try{MyThread thread=NewMyThread (); Thread.Start (); Thread.Sleep (200); Thread.Interrupt (); } Catch(interruptedexception e) {System.out.println ("Main Catch"); E.printstacktrace (); } System.out.println ("End!"); }}
Results:
3, Violence Stop Stop () (deprecated method, not recommended)
Attention:
(1) The cessation of violence may lead to the completion of the cleanup work.
(2), resulting in data not synchronized processing, resulting in inconsistent data issues.
Public classMyThreadextendsThread {Private inti = 0; @Override Public voidrun () {Try { while(true) {i++; System.out.println ("I=" +i); Thread.Sleep (1000); } } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public Static voidMain (string[] args) {Try{MyThread thread=NewMyThread (); Thread.Start (); Thread.Sleep (8000); Thread.stop (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
4. Use Ruturn to stop the thread
Public classMyThreadextendsThread {@Override Public voidrun () { while(true) { if( This. isinterrupted ()) {System.out.println ("Stop it!"); return; } System.out.println ("Timer=" +System.currenttimemillis ()); } } Public Static voidMain (string[] args)throwsinterruptedexception {MyThread T=NewMyThread (); T.start (); Thread.Sleep (2000); T.interrupt (); }}
Results:
Two
(1) Advantages and disadvantages of suspend and resume:
Cons: (1) exclusive-improper use, very easy to cause the public synchronization object exclusive, is other threads cannot access the common synchronization object.
(2) Out of sync-situations where data is not synchronized because of thread pauses.
(2), Yield () method: Discard the current CPU resources, let other tasks to occupy CPU resources. Abandoned time is uncertain, may just give up, and immediately get the CPU time slice.
Second, the Java Multithreaded Programming core technology (note)--How to stop the thread?