阅读背景:

JAVA NIO源码分析 之Selector

来源:互联网 

      先上一段代码

 public static void main(String[] args) throws Exception{

        Selector selector = SelectorProvider.provider().openSelector();
        ServerSocketChannel serverSocketChannel = ServerSocketChannel
                .open()
                .bind(new InetSocketAddress("127.0.0.1",9999));
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("等待连接");
        while (true) {
            while (selector.select()>0){
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                for (SelectionKey key:selectionKeys) {
                    if (key.isAcceptable()){
                        System.out.println("接受");
                        selectionKeys.remove(key);
                        System.out.println(selector.select());
                        System.out.println(selector.select());
                    }
                }
            }

        }


    } public static void main(Str



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: