SocketIO---bio2---带线程池处理任务
生活随笔
收集整理的這篇文章主要介紹了
SocketIO---bio2---带线程池处理任务
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. server 先啟動(dòng)
1 public class Server { 2 public static final int port = 8765; 3 4 public static void main(String[] args) { 5 System.out.println("Server start...\n"); 6 Server server = new Server(); 7 server.init(); 8 9 } 10 11 public void init() { 12 try { 13 HandlerExecutorPool executorPool = new HandlerExecutorPool(50, 1000); 14 ServerSocket serverSocket = new ServerSocket(port); //監(jiān)聽(tīng)端口 15 while(true){ 16 Socket socket = serverSocket.accept(); //接收客戶端的請(qǐng)求數(shù)據(jù) 17 executorPool.execute(new HandlerThread(socket)); 18 //new Thread(new HandlerThread(socket)).start(); //線程去處理請(qǐng)求 19 } 20 } catch (IOException e) { 21 System.out.println("服務(wù)器異常: " + e.getMessage()); 22 } 23 } 24 } View Code2. server 處理任務(wù)的線程
1 public class HandlerThread implements Runnable { 2 private Socket socket; 3 public HandlerThread(Socket socket){ 4 this.socket = socket; 5 } 6 7 @Override 8 public void run() { 9 try { 10 BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 11 String clientStr = input.readLine(); 12 System.out.println("服務(wù)端接收到客戶端請(qǐng)求的數(shù)據(jù)為:"+clientStr); 13 14 PrintStream out = new PrintStream(socket.getOutputStream()); 15 System.out.println("服務(wù)端請(qǐng)輸入響應(yīng):\t"); 16 String responseStr = new BufferedReader(new InputStreamReader(System.in)).readLine(); 17 out.println(responseStr); 18 19 out.close(); 20 input.close(); 21 22 } catch (IOException e) { 23 System.out.println("服務(wù)器 run 異常: " + e.getMessage()); 24 } finally { 25 if (socket != null) { 26 try { 27 socket.close(); 28 } catch (Exception e) { 29 socket = null; 30 System.out.println("服務(wù)端 finally 異常:" + e.getMessage()); 31 } 32 } 33 } 34 35 } 36 37 } View Code3. 線程池類(lèi)
1 public class HandlerExecutorPool { 2 3 private ExecutorService executor; 4 public HandlerExecutorPool(int maxPoolSize, int queueSize){ 5 this.executor = new ThreadPoolExecutor( 6 Runtime.getRuntime().availableProcessors(),//corePoolSize 7 maxPoolSize, 8 120L, //keepAliveTime 9 TimeUnit.SECONDS, 10 new ArrayBlockingQueue<Runnable>(queueSize)); 11 } 12 13 public void execute(Runnable task){ 14 this.executor.execute(task); 15 } 16 } View Code4. 客戶端
1 public class Client { 2 3 private static final int port = 8765; 4 //private static final String host = "localhost"; 可以 5 //private static final String host = "127.0.0.1"; 可以 6 private static final String host = "192.168.233.1"; //可以 7 8 public static void main(String[] args) { 9 System.out.println("Client start..."); 10 while(true){ 11 Socket socket = null; 12 try { 13 socket = new Socket(host, port); 14 BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 15 PrintStream out = new PrintStream(socket.getOutputStream()); 16 System.out.println("客戶端請(qǐng)輸入請(qǐng)求:\t"); 17 String str = new BufferedReader(new InputStreamReader(System.in)).readLine(); 18 out.println(str); 19 20 String result = input.readLine(); 21 System.out.println("客戶端接收到服務(wù)器端響應(yīng)的數(shù)據(jù):" + result); 22 if("ok".equalsIgnoreCase(result)){ 23 System.out.println("客戶端將要關(guān)閉連接"); 24 Thread.sleep(500); 25 break; 26 } 27 out.close(); 28 input.close(); 29 30 } catch (Exception e) { 31 System.out.println("客戶端異常:" + e.getMessage()); 32 } finally { 33 if(null != socket){ 34 try { 35 socket.close(); 36 } catch (IOException e) { 37 socket = null; 38 System.out.println("客戶端finally異常:"+e); 39 } 40 } 41 } 42 } 43 } 44 } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/bravolove/p/8035303.html
總結(jié)
以上是生活随笔為你收集整理的SocketIO---bio2---带线程池处理任务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HDU_oj_2047 阿牛的EOF牛
- 下一篇: Sublime Text3 配置设置攻略