20110801

java se 7 AIO的实现

 

windows基于IOCP(input/output Completion Port,I/O完成端口它是应用程序使用线程池处理异步I/O请求的一种机制.

 

linux基于epoll (EpollPort)

 

编程模型

AIO模型相对于之前的编程模型,更加简便,且把线程池隐藏在内部实现中。

 

服务器端

 

channelGroup = AsynchronousChannelGroup.withFixedThreadPool(Runtime.getRuntime().availableProcessors(),
                                                                    Executors.defaultThreadFactory());

 final AsynchronousServerSocketChannel listener = openChannel(channelGroup);
        listener.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        listener.bind(new InetSocketAddress(port));


        listener.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {

            @Override
            public void completed(AsynchronousSocketChannel result, Void attachment) {
                // request a new accept and handle the incoming connection
                listener.accept(null, this);
                System.out.println("new connection" + result);
                handleNewConnection(result);
            }

            @Override
            public void failed(Throwable exc, Void attachment) {
            }
        });
handleNewConnection
        ByteBuffer input = ByteBuffer.allocate(256);
        if (!channel.isOpen()) {
            return;
        }
        channel.read(input, input, completionHandler);
 

 

客户端

 

AsynchronousChannelGroup group;
        try {
            group = AsynchronousChannelGroup.withFixedThreadPool(2, Executors.defaultThreadFactory());

            final AsynchronousSocketChannel asc = AsynchronousSocketChannel.open(group);

            asc.connect(new InetSocketAddress(InetAddress.getByName("localhost"), 5000), null,
                        new CompletionHandler<Void, Void>() {

                            /*
                             * (non-Javadoc)
                             * @see java.nio.channels.CompletionHandler#completed(java.lang.Object, java.lang.Object)
                             */
                            @Override
                            public void completed(Void result, Void attachment) {
                                System.out.println("connected server.");

                            }

                            /*
                             * (non-Javadoc)
                             * @see java.nio.channels.CompletionHandler#failed(java.lang.Throwable, java.lang.Object)
                             */
                            @Override
                            public void failed(Throwable exc, Void attachment) {
                                // TODO Auto-generated method stub

                            }

                        });

            // 8K
            ByteBuffer bb = ByteBuffer.allocate(2 * 1024);
            asc.read(bb, bb, new CompletionHandler<Integer, ByteBuffer>() {

                /*
                 * (non-Javadoc)
                 * @see java.nio.channels.CompletionHandler#completed(java.lang.Object, java.lang.Object)
                 */
                @Override
                public void completed(Integer result, ByteBuffer bb) {
                    System.out.println(new String(bb.array(), 0, result));

                    bb.flip();
                    asc.read(bb, bb, this);
                }

                /*
                 * (non-Javadoc)
                 * @see java.nio.channels.CompletionHandler#failed(java.lang.Throwable, java.lang.Object)
                 */
                @Override
                public void failed(Throwable exc, ByteBuffer attachment) {
                    // TODO Auto-generated method stub

                }

            });

            byte[] bytes = new byte[1024];
            int result = System.in.read(bytes);

            ByteBuffer bb1 = ByteBuffer.wrap(bytes, 0, result);

            asc.write(bb1, bb1, new CompletionHandler<Integer, ByteBuffer>() {

                /*
                 * (non-Javadoc)
                 * @see java.nio.channels.CompletionHandler#completed(java.lang.Object, java.lang.Object)
                 */
                @Override
                public void completed(Integer result, ByteBuffer attachment) {
                    byte[] bytes = new byte[1024];
                    try {
                        int result1 = System.in.read(bytes);

                        ByteBuffer bb1 = ByteBuffer.wrap(bytes, 0, result1);

                        asc.write(bb1, bb1, this);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                /*
                 * (non-Javadoc)
                 * @see java.nio.channels.CompletionHandler#failed(java.lang.Throwable, java.lang.Object)
                 */
                @Override
                public void failed(Throwable exc, ByteBuffer attachment) {
                    // TODO Auto-generated method stub

                }

            });

            // group.shutdownNow();
            // group.awaitTermination(1, TimeUnit.SECONDS);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值