SocketIO---bio2---带线程池处理任务

Posted 黑土白云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SocketIO---bio2---带线程池处理任务相关的知识,希望对你有一定的参考价值。

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---带线程池处理任务的主要内容,如果未能解决你的问题,请参考以下文章

线程池

线程池

线程池线程池的原理

什么是线程池

线程池

线程池专题(自学)