There are three ways to make a terminating thread.
1. Use the exit flag to cause the thread to exit normally, that is, the thread terminates when the Run method completes.
2. Use the Stop method to forcibly terminate the thread (this method is not recommended because stop and suspend, resume, can also cause unpredictable results).
3. Use the interrupt method to break the thread.
1. Terminating a thread with an exit flag
When the Run method finishes executing, the thread exits. But sometimes the run method is never finished. Use threads to listen for client requests in a server-side program, or other tasks that require cyclic processing. In this case, it is common to place these tasks in a loop, such as a while loop. If you want the loop to run forever, you can use while (True) {...} To deal with. However, the most straightforward way to make a while loop exit under a particular condition is to set a Boolean flag and control whether the while loop exits by setting this flag to true or false. An example of terminating a thread with an exit flag is given below.
Public classThreadflagextendsThread { Public volatile BooleanExit =false; Public voidrun () { while(!exit); } Public Static voidMain (string[] args)throwsException {threadflag thread=NewThreadflag (); Thread.Start (); Sleep (5000);//main thread delay 5 secondsThread.exit =true;//Terminating Threads threadThread.Join (); System.out.println ("Thread quits!"); } }
An exit flag is defined in the preceding code exit, and when Exit is true, the while loop exits, The default value for Exit is false. When you define exit, a Java keyword, volatile, is used to synchronize exit, meaning that only one thread can modify the value of exit at the same time.
2. Terminating a thread by using the Stop method
Use the Stop method to forcibly terminate a running or suspended thread. We can use the following code to terminate the thread:
Thread.stop ();
Although using the above code can terminate a thread, using the Stop method is dangerous, just like suddenly shutting down the computer, rather than shutting down as a normal program, it is not recommended to use the Stop method to terminate the thread.
3. Terminating a thread using the interrupt method
Using the interrupt method to end a thread can be divided into two situations:
(1) The thread is in a blocked state, such as using the Sleep method.
(2) Use while (! Isinterrupted ()) {...} To determine if a thread has been interrupted.
In the first case, using the interrupt method, the sleep method throws a Interruptedexception exception, and in the second case the thread exits directly. The following code demonstrates the use of the interrupt method in the first case.
Public classThreadinterruptextendsThread { Public voidrun () {Try{sleep (50000);//50 seconds Delay } Catch(interruptedexception e) {System.out.println (E.getmessage ()); } } Public Static voidMain (string[] args)throwsException {thread thread=NewThreadinterrupt (); Thread.Start (); System.out.println ("Press any key within 50 seconds to break the thread!"); System.in.read (); Thread.Interrupt (); Thread.Join (); System.out.println ("Thread has exited!"); } }
The result of the above code is as follows:
Press any key within 50 seconds to break the thread! sleep interrupted thread has exited !
After calling the interrupt method, the Sleep method throws an exception and then outputs the error message: Sleep interrupted.
Note: There are two methods in the thread class to determine whether a thread is terminated through the interrupt method. One is static method interrupted (), a non-static method isinterrupted (), the difference between the two methods is interrupted to determine whether the current line is interrupted, and isinterrupted can be used to determine whether other threads are interrupted. So while (! Isinterrupted ()) can also be replaced by while (! Thread.interrupted ()).
Original link: Three ways to terminate a thread
Three ways to terminate a thread in "Java"