1. 程式人生 > >SocketIO---bio2---帶線程池處理任務

SocketIO---bio2---帶線程池處理任務

response 線程池 ali live stream lex pos blog 技術

1. server 先啟動

技術分享圖片
 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); //監聽端口 15 while(true){ 16 Socket socket = serverSocket.accept(); //接收客戶端的請求數據 17 executorPool.execute(new
HandlerThread(socket)); 18 //new Thread(new HandlerThread(socket)).start(); //線程去處理請求 19 } 20 } catch (IOException e) { 21 System.out.println("服務器異常: " + e.getMessage()); 22 } 23 } 24 }
View Code

2. server 處理任務的線程

技術分享圖片
 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("服務端接收到客戶端請求的數據為:"+clientStr); 13 14 PrintStream out = new PrintStream(socket.getOutputStream()); 15 System.out.println("服務端請輸入響應:\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("服務器 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("服務端 finally 異常:" + e.getMessage()); 31 } 32 } 33 } 34 35 } 36 37 }
View Code

3. 線程池類

技術分享圖片
 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 Code

4. 客戶端

技術分享圖片
 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("客戶端請輸入請求:\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("客戶端接收到服務器端響應的數據:" + result);
22                 if("ok".equalsIgnoreCase(result)){
23                     System.out.println("客戶端將要關閉連接");
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

SocketIO---bio2---帶線程池處理任務