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(); } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/15856.html