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);

你可能感兴趣的:(20110801)