Java代碼:
package Threads;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* Created by Frank
*/
public class StopClose extends Thread {
protected Socket io;
public void run() {
try {
io = new Socket("java.sun.com", 80);
BufferedReader is = new BufferedReader(new InputStreamReader(io.getInputStream()));
System.out.println("StopClose reading");
/**
* 死鎖,因為讀取響應之前,HTTP責成客戶端發送一個請求(像GET/HTTP/1.0)和一個空行
*/
String line = is.readLine();
/**
* 所以我們永遠不可能到達這里
*/
System.out.printf("StopClose FINISHED after reading %s!", line);
} catch (IOException ex) {
System.out.println("StopClose terminating:" + ex);
}
}
public void shutDown() throws IOException {
if (io != null) {
synchronized (io) {
io.close();
}
}
System.out.println("StopClose.shutDown() complete");
}
public static void main(String[] args) throws InterruptedException, IOException {
StopClose t = new StopClose();
t.start();
sleep(1000*5);
t.shutDown();
}
}

