Find a way to stop the thread from running, the code is as follows:
Public classStopthreadextendsThread {Private volatile BooleanStop =false; @Override Public voidrun () {System.out.println ("T1 is start ..."); while(!stop) {System.out.println ("T1 is running ...."); } System.out.println ("T1 stopped"); } Public voidTerminate () {Stop=true; } Public Static voidMain (string[] args) {stopthread T1=NewStopthread (); T1.start (); for(inti = 0; I < 500000;i++) {System.out.println ("Main is running ...."); } t1.terminate (); }}
You can then stop the thread that is running.
But there's a problem with that, and what I'm writing now is that I'm going to go back to the while and judge the condition so that it jumps out. However, if you want to control the thread execution time, if the timeout is not executed, return directly, this code is still inappropriate. Can think again ....
Stop a Java thread from executing