Java通過關閉Socket終止線程
更新時間:2017年04月10日 09:12:48 作者:FrankYou
這篇文章主要為大家詳細介紹了Java通過關閉Socket終止線程的相關代碼
本文實例為大家分享了Java關閉Socket實現(xiàn)終止線程的具體代碼,供大家參考,具體內(nèi)容如下
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責成客戶端發(fā)送一個請求(像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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合新版SpringSecurity完整過程
Spring Security是保障Spring應用程序安全的強大框架,而新版的Spring Security引入了lambda表達式來配置,使得安全配置更加簡潔、優(yōu)雅,本文將介紹如何在Spring Boot項目中整合新版Spring Security,需要的朋友可以參考下2024-02-02