为了解决BIO(同步阻塞IO)带来的一个链路需要一个线程处理的问题,后来有人对它的线程模型进行了优化,后端通过一个线程池来处理多个客户端的接入,从而形成了一个客户端个数M:线程池最大数N的关系。M可以远大于N,通过线程池可以灵活的调整线程资源,设置线程的最大值,防止由于大量并发导致资源耗尽。

服务端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * @author j.tommy
 * Created by j.tommy on 2017/11/11.
 */
public class TimeServer {
    private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
    private ExecutorService es = new ThreadPoolExecutor(1,100,0L, TimeUnit.MICROSECONDS,new LinkedBlockingQueue<Runnable>(1024),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) {
        TimeServer timeServer = new TimeServer();
        timeServer.start(8080);
    }
    private void start(int port) {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(port);
            System.out.println("TimeServer is running...");
            while (true) {
                Socket socket = ss.accept();
                es.submit(new TimeServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                es.shutdownNow();
            }
        }
    }
    class TimeServerHandler implements Runnable {
        private Socket socket = null;
        public TimeServerHandler(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            BufferedReader br = null;
            PrintWriter pw = null;
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
                while (true) {
                    String input = br.readLine();
                    if (null == input) {
                        break;
                    }
                    System.out.println("接收到客户端请求:" + input);
                    if ("Server Time".equalsIgnoreCase(input)) {
                        pw.println((new Date()).toString());
                    }
                }
            } catch (IOException e) {
                System.out.println("ip:" + socket.getInetAddress().getHostAddress() + " I/O异常");
            }
            finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (pw != null) {
                    pw.close();
                }
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

客户端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * @author j.tommy
 * Created by j.tommy on 2017/11/11.
 */
public class TimeClient {
    public static void main(String[] args) {
        TimeClient tc = new TimeClient();
        tc.connect("127.0.0.1",8080);
    }
    private void connect(String host,int port) {
        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;
        try {
            socket = new Socket(host,port);
            in = socket.getInputStream();
            out = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out),true);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            System.out.println("请求服务器时间");
            pw.println("Server Time");
            String response = br.readLine();
            System.out.println("response:" + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

伪异步IO通过线程池+任务队列来实现,当有新的客户端接入时,将客户端封装成一个新的Task,交给线程池处理。由于线程池和任务队列的大小都是可控制的,所以不论有多少个客户端访问,都不会导致资源耗尽和宕机。

伪异步IO的弊端:
当对Socket的输入流进行读取时,它会一直阻塞,直到发生下面3种事件之一:
1.有数据可读;
2.可用数据已经读取完毕;
3.发生空指针或IO异常。

这就意味着,当对方发送请求或应答消息缓慢、或者网络传输较慢时,读取输入流一方的线程将被长时间阻塞。在此期间,其他接入消息只能在任务队列中排队。

参考《Netty权威指南》