伪异步IO编程

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

服务端代码:

/**
 * @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();
                    }
                }
            }
        }
    }
}

客户端代码:

/**
 * @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权威指南》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值