First, Thread.stop ()
Officially not recommended, Because it is inherently unsafe.
Two, mode one
1. Threading Class Example
Public classThreadT1 implements Runnable {PrivateThread Threadthis; Public voidstart () {Threadthis=NewThread ( This); Threadthis.start (); } Public voidStop () {threadthis=NULL; } Public voidrun () {Thread thisthread=Thread.CurrentThread (); while(Threadthis = =thisthread) {System. out. println ("lalala~"); //Do YOUR work!} System. out. println ("stopped!!!"); }}
2. Using the example
New// Create thread and start / Stop this thread
Three, mode two
1. Threading Class Example
Public classThreadT2 extends Thread {PrivateBoolean stop =false; Public voidStopbymark () {Stop=true; } Public voidrun () { while(!stop) {System. out. println ("lalala~"); //Do YOUR work!} System. out. println ("stopped!!!"); }}
2. Using the example
New// Create thread and start / Stop this thread
Attached, about Thread.Interrupt ()
The thread can also be stopped in this way.
Prerequisites:
Public void run () { try { thread.sleep (); Catch // The method that captures the interruptedexception must exist, and the stop method is called when the throw occurs. e.printstacktrace (); Stopbymark (); }}
Methods for external threads to stop Java child threads