Interrupting I/O operations
However, what happens if the thread is blocked while the I/O operation is in progress? I/O operations can block threads for considerable periods of time, especially when network applications are involved. For example, a server may need to wait for a request, or a network application may need to wait for a response from a remote host.
If you are using channels (which is the new I/O API introduced in Java 1.4), then the blocked thread will receive a ClosedByInterruptException exception. If this is the case, the logic of the code is the same as in the third example, just the exception is different.
However, you may be using traditional I/O that existed before Java 1.0 and requires more work. In this case, Thread.interrupt() will not work because the thread will not exit the blocked state. Listing D describes this behavior. Although interrupt() is called, the thread will not exit the blocked state.
Listing D import java.io.*; class Example4 extends Thread { public static void main( String args[] ) throws Exception { Example4 thread = new Example4(); System.out.println( "Starting thread..." ); thread.start(); Thread.sleep( 3000 ); System.out.println( "Interrupting thread..." ); thread.interrupt(); Thread.sleep( 3000 ); System.out.println( "Stopping application..." ); //System.exit( 0 ); } public void run() { ServerSocket socket; try { socket = new ServerSocket(7856); } catch ( IOException e ) { System.out.println( "Could not create the socket..." ); return; } while ( true ) { System.out.println( "Waiting for connection..." ); try { Socket sock = socket.accept(); } catch ( IOException e ) { System.out.println( "accept() failed or interrupted..." ); } } } }
Fortunately, the Java platform provides a solution for this situation, which is to call the close() method of the socket that blocks the thread. In this case, if the thread is blocked by an I/O operation, the thread will receive a SocketException, which is very similar to using the interrupt() method to cause an InterruptedException to be thrown.
The only thing to note is that there must be a reference to the socket, only in this way can the close() method be called. This means that the socket object must be shared. Listing E describes this situation. The running logic is the same as the previous example.
Listing E import java.net.*; import java.io.*; class Example5 extends Thread { volatile boolean stop = false; volatile ServerSocket socket; public static void main( String args[] ) throws Exception { Example5 thread = new Example5(); 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..." ); //System.exit( 0 ); } public void run() { try { socket = new ServerSocket(7856); } catch ( IOException e ) { System.out.println( "Could 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 following is the output after running the code in Listing E:
Starting thread... Waiting for connection... Asking thread to stop... accept() failed or interrupted... Thread exiting under request... Stopping application...
Multithreading is a powerful tool, but it is presenting a series of difficulties. One of them is how to interrupt a running thread. If implemented properly, interrupting threads using the above techniques is simpler than using the inline operations already provided on the Java platform.
The above is the content of how to interrupt a running thread (3) in Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!