终止正在运行的线程
中断I/O操作
然而,如果线程在I/O操作进行时被阻塞,又会如何?I/O操作可以阻塞线程一段相当长的时间,特别是牵扯到网络应用时。例如,服务器可能需要等待一个请求(request),又或者,一个网络应用程序可能要等待远端主机的响应。
如果你正使用通道(channels)(这是在Java 1.4中引入的新的I/O API),那么被阻塞的线程将收到一个 ClosedByInterruptException异常。如果情况是这样,其代码的逻辑和第三个例子中的是一样的,只是异常不同而已。
但是,你可能正使用Java1.0之前就存在的传统的I/O,而且要求更多的工作。既然这样,Thread.interrupt()将不起作用,因为线程将不会退出被阻塞状态。Listing D描述了这一行为。尽管interrupt()被调用,线程也不会退出被阻塞状态。
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..." );
????? }
??? }
? }
}
很幸运,Java平台为这种情形提供了一项解决方案,即调用阻塞该线程的套接字的close()方法。在这种情形下,如果线程被I/O操作阻塞,该线程将接收到一个SocketException异常,这与使用interrupt()方法引起一个InterruptedException异常被抛出非常相似。
唯一要说明的是,必须存在socket的引用(reference),只有这样close()方法才能被调用。这意味着socket对象必须被共享。Listing E描述了这一情形。运行逻辑和以前的示例是相同的。
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..." );
? }
}
以下是运行Listing E中代码后的输出:
Starting thread...
Waiting for connection...
Asking thread to stop...
accept() failed or interrupted...
Thread exiting under request...
Stopping application...
多线程是一个强大的工具,然而它正呈现出一系列难题。其中之一是如何中断一个正在运行的线程。如果恰当地实现,使用上述技术中断线程将比使用Java平台上已经提供的内嵌操作更为简单。