Interrupt I/O operations
However, what if the thread is blocked during I/O operations? I/O operations can block threads for a long period of time, especially when network applications are involved. For example, a server may have to wait for a request, or a network application may have to wait for the response from a remote host.
If you are using channel (channels) (this is the new I/O API introduced in Java 1.4), the blocked thread will receive a closedbyinterruptexception exception. In this case, the logic of the Code is the same as that in the third example, but the exception is different.
However, you may be using the traditional I/O that existed before java1.0, and require more work. In this case, thread. Interrupt () does not work because the thread will not exit the blocked state. Listing D describes this line. Although interrupt () is called, the thread does not exit the blocked state.
Listing Dimport 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, that is, to 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.
The only thing to note is that the reference of the socket must exist. 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 Eimport 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 output after running the code in listing e is as follows:
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 presents a series of difficulties. One of them is how to interrupt a running thread. If implemented properly, using the above technology to interrupt the thread will be easier than using the embedded operations already provided on the Java platform.
Original article: http://developer.51cto.com/art/200812/100482_2.htm