IO vs NIO vs NIO.2

I/O vs NI/O vs NIO.2

NIO 'N' 是 New 还是 Non-Blocking?

IO

IO


这里我们分别用BlockingIO 和 Non-BlockingIO实现 EchoServer:

Blocking IO

public class PlainEchoServer {

    public void serve(int port) throws IOException {
        final ServerSocket socket = new ServerSocket(port);              //#1
        try {
            while (true) {
                final Socket clientSocket = socket.accept();             //#2
                System.out.println("Accepted connection from " + clientSocket);
                new Thread(new Runnable() {                              //#3
                    @Override
                    public void run() {
                        try {
                            BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                            PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
                            while(true) {                                //#4
                                writer.println(reader.readLine());
                                writer.flush();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            try {
                                clientSocket.close();
                            } catch (IOException ex) {
                                // ignore on close
                            }
                        } }
                }).start();                                              //#5
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        new PlainEchoServer().serve(8989);
    }
}
  • 1 :绑定端口
  • 2 :会一直阻塞直到下一个客户端链接;
  • 3 :这里会创建一个新的线程来处理客户端链接
  • 4 :接收数据并写回
  • 5 :启动线程

这里我们必须为每一个客户端链接开启一个单独的线程,虽然,我们可以使用线程池去处理,但是并没有从本质上解决这个问题。


NIO

NIO

Non-Blocking IO
其实在 "NIO-异步IO"中已经实现过EchoServer,可以参考。

public class PlainNioEchoServer {
    public void serve(int port) throws IOException {
        System.out.println("Listening for connections on port " + port);

        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        ServerSocket ss = serverChannel.socket();
        InetSocketAddress address = new InetSocketAddress(port);
        ss.bind(address);                                                //#1
        serverChannel.configureBlocking(false);
        Selector selector = Selector.open();
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);        //#2
        while (true) {
            try {
                selector.select();                                       //#3
            } catch (IOException ex) {
                ex.printStackTrace();
                // handle in a proper way
                break;
            }
            Set readyKeys = selector.selectedKeys();                     //#4
            Iterator iterator = readyKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = (SelectionKey) iterator.next();
                iterator.remove();                                       //#5
                try {
                    if (key.isAcceptable()) {
                        ServerSocketChannel server = (ServerSocketChannel)key.channel();
                        SocketChannel client = server.accept();          //#6
                        System.out.println("Accepted connection from : " + client);
                        client.configureBlocking(false);
                        client.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, ByteBuffer.allocate(100));              //#7
                    }
                    if (key.isReadable()) {                              //#8
                        SocketChannel client = (SocketChannel) key.channel();
                        ByteBuffer output = (ByteBuffer) key.attachment();
                        client.read(output);                             //#9
                    }
                    if (key.isWritable()) {                             //#10
                        SocketChannel client = (SocketChannel) key.channel();
                        ByteBuffer output = (ByteBuffer) key.attachment();
                        output.flip();
                        client.write(output);                           //#11
                        output.compact();
                    }
                } catch (IOException ex) {
                    key.cancel();
                    try {
                        key.channel().close();
                    } catch (IOException cex) {
                    }
                } }
        }
    }

    public static void main(String[] args) throws IOException {
        new PlainNioEchoServer().serve(8989);
    }
}
  • 1 :绑定端口
  • 2 :服务通道上注册监听Accept事件,这样新链接进来便可以感知到
  • 3 :这里会阻塞
  • 4 :获取到所有的SelectKey实例
  • 5 :将已经处理的实例从注册表中删除
  • 6 :接收客户端链接
  • 7 :注册链接到selector上
  • 8 :检查读
  • 9 :从缓冲区读到通道
  • 10:检测写
  • 11:从缓冲区写数据到通道

NIO.2

NIO.2 提供了一个CompletionHandler,这个handler会在操作(Accept、Read、Write)完成时回调,不用像我们在NIO的例子中那样做检测。

public class PlainNio2EchoServer {

    public void serve(int port) throws IOException {
        System.out.println("Listening for connections on port " + port);
        final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open();
        InetSocketAddress address = new InetSocketAddress(port);
        serverChannel.bind(address);                                    //#1

        final CountDownLatch latch = new CountDownLatch(1);

        serverChannel.accept(
                null,
                new CompletionHandler<AsynchronousSocketChannel, Object>() { //#2
                    @Override
                    public void completed(final AsynchronousSocketChannel channel, Object attachment) {
                        serverChannel.accept(null, this);                 //#3
                        ByteBuffer buffer = ByteBuffer.allocate(100);
                        channel.read(buffer, buffer, new EchoCompletionHandler(channel));  //#4
                    }

                    @Override
                    public void failed(Throwable throwable, Object attachment) {
                        try {
                            serverChannel.close();                              //#5
                        } catch (IOException e) {
                            // ingnore on close
                        } finally {
                            latch.countDown();
                        }
                    }
                }
        );

        try {
            latch.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    private final class EchoCompletionHandler implements CompletionHandler<Integer, ByteBuffer> {
        private final AsynchronousSocketChannel channel;

        EchoCompletionHandler(AsynchronousSocketChannel channel) {
            this.channel = channel;
        }

        @Override
        public void completed(Integer result, ByteBuffer buffer) {
            buffer.flip();
            channel.write(buffer, buffer, new CompletionHandler<Integer, ByteBuffer>() { //#6
                @Override
                public void completed(Integer result, ByteBuffer buffer) {
                    if (buffer.hasRemaining()) {
                        channel.write(buffer, buffer, this); //#7
                    } else {
                        buffer.compact();
                        channel.read(buffer, buffer,this);   //#8
                    }
                }

                @Override
                public void failed(Throwable exc, ByteBuffer attachment) {
                    try {
                        channel.close();
                    } catch (IOException e) {
                        // ingnore on close
                    }
                }
            });
        }

        @Override
        public void failed(Throwable exc, ByteBuffer attachment) {
            try {
                channel.close();
            } catch (IOException e) {
                // ingnore on close
            }
        }
    }


    public static void main(String[] args) throws IOException {
        new PlainNio2EchoServer().serve(8989);
    }

}
  • 1 :绑定端口
  • 2 :开始接收客户端链接,一有链接便会回调handler
  • 3 :使用同一个handler,在此准备接收新的客户端链接
  • 4 :触发一个通道的读操作,并注册一个EchoHandler回调
  • 5 :出现错误则关闭通道
  • 6 :触发一个通道的写操作,并注册当前EchoHandler回调
  • 7 :缓冲区中仍然有数据的话,同样触发一个写操作
  • 8 :触发一个通道的读操作,并注册当前EchoHandler回调
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,386评论 19 139
  • NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型,也...
    闪电是只猫阅读 8,334评论 0 7
  • Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java I...
    JackChen1024阅读 12,211评论 1 143
  • 这两天了解了一下关于NIO方面的知识,网上关于这一块的介绍只是介绍了一下基本用法,没有系统的解释NIO与阻塞、非阻...
    Ruheng阅读 11,930评论 5 48
  • 每天起床第一件事,不是上厕所,而是拉开窗帘跟我的植物们互道早安。 下班到家后也会抱着她们看,叶片长了新芽,结了小小...
    和猫旅行阅读 1,391评论 0 0