Interrupt method for stopping Java threads
I have recently learned Java multithreading-related knowledge points. Many experts on the Internet have provided detailed answers to the thread stop methods, here, I will remind myself of the Interrupt method.
First, let's roughly list the methods for stopping a thread:
1. Use the stop () method, but it is no longer recommended, just like suspend and resume.
2. Use the exit sign to terminate the thread, introduce a shared variable, volatile type, or use synchronized to monitor methods related to operations on shared variables. Then, in the run () method, this flag is poll continuously through the while loop.
3. Use the Interrupt method to Interrupt the thread.
Note: When I first saw this method, I thought that interrupt will stop the thread, but in fact it is not the case. Calling the Interrupt method of a thread will change the thread state to the interrupt state. This can be subdivided into two aspects:
1) For threads whose sleep, wait, and join methods are executed, calling the Interrupt method will stop them from sleep and throw an InterruptedException exception. For example, if thread A is in sleep and another program calls the interrupt method of thread A, it will force thread A to stop sleep and throw an InterruptedException exception, thus escaping the blocking state in advance.
2) For A running thread, that is, A thread without blocking, calling the Interrupt method changes the status of thread A to interruptted without affecting the execution of thread.
The following is an example of using the Interrupt method for a thread in the blocking state:
Class MyThread extends Thread {
Volatile boolean stop = false;
Public static void main (String args []) throws Exception {
MyThread thread = new MyThread ();
System. out. println ("Starting thread ...");
Thread. start ();
Thread. sleep (3000 );
System. out. println ("hread stop ...");
Thread. stop = true; // If the thread is blocked, this variable is not checked.
Thread. interrupt ();
Thread. sleep (3000 );
System. out. println ("end ...");
}
Public void run (){
While (! Stop ){
System. out. println ("Thread running ...");
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
System. out. println ("Thread interrupted ...");
}
}
System. out. println ("Thread exiting under request ...");
}
}
Note: For threads blocked by the preceding methods, the correct way to stop the thread is to set the shared variable and call interrupt () (note that the variable should be set first ). The shared variable is set to ensure normal exit of the thread without being blocked.
The following content is still being studied
When reading some blogs, I also saw some special situations. I also want to add a supplement here:
The thread is blocked during I/O operations.
There are two situations:
1) when channel (channels) is used, the channel is the I/O API introduced by Java 1.4, And the blocked thread will receive a ClosedByInterruptException exception. This is the same as the use of the Interrupt method to throw an exception, but the exception is different.
2) use traditional I/O.
Note: even if the Interrupt method is called, Thread. interrupt () does not work because the Thread will not exit the blocked state.
Solution: Call the close () method of the socket blocking this thread. In this case, if the thread is blocked by I/O operations, the thread will receive a SocketException, which is very similar to the InterruptedException exception thrown by the interrupt () method. Note: The only thing to note is that the socket reference must exist. Only in this way can the close () method be called. This means that the socket object must be shared. The following code is for reference:
Class MyThread extends Thread {
Volatile boolean stop = false;
Volatile ServerSocket socket;
Public static void main (String args []) throws Exception {
MyThread thread = new MyThread ();
System. out. println ("Starting thread ...");
Thread. start ();
Thread. sleep (3000 );
System. out. println ("Asking thread to stop ...");
Thread. stop = true;
Thread. socket. close ();
Thread. sleep (3000 );
System. out. println ("Stopping application ...");
}
Public void run (){
Try {
Socket = new ServerSocket (7856 );
} Catch (IOException e ){
System. out. println ("cocould not create the socket ...");
Return;
}
While (! Stop ){
System. out. println ("Waiting for connection ...");
Try {
Socket sock = socket. accept ();
} Catch (IOException e ){
System. out. println ("accept () failed or interrupted ...");
}
}
System. out. println ("Thread exiting under request ...");
}
}
The above is my summary of the thread stop method. Of course, it also draws on the sharing of many bloggers. The focus is on the Interrupt method, because it is easy to understand errors!