@@ -0,0 +1,3715 @@
+package com.hzh.corejava.nio;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.io.IOException;%0Aimport java.net.InetSocketAddress;%0Aimport java.net.Socket;%0Aimport java.nio.channels.*;%0Aimport java.util.Iterator;%0Aimport java.util.Set;%0A%0A/**%0A * Created by huangzehai on 2017/3/13.%0A */%0Apublic class SelectorExample extends Thread %7B%0A    private Logger logger = LoggerFactory.getLogger(SelectorExample.class);%0A    private ServerSocketChannel channel;%0A    private Selector selector;%0A    private volatile boolean running = false;%0A%0A    public static void main(String args%5B%5D) throws IOException %7B%0A        SelectorExample server = new SelectorExample();%0A        try %7B%0A            server.startServer();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A        try %7B%0A            Thread.sleep(3000);%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        server.stopServer();%0A    %7D%0A%0A    public void startServer() %7B%0A        this.start();%0A    %7D%0A%0A    public void stopServer() %7B%0A        running = false;%0A        if (this.channel != null) %7B%0A            try %7B%0A                this.channel.close();%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A%0A        try %7B%0A            Iterator%3CSelectionKey%3E keys = this.selector.keys().iterator();%0A            while (keys.hasNext()) %7B%0A                SelectionKey key = keys.next();%0A                SelectableChannel channel = key.channel();%0A                if (channel instanceof SocketChannel) %7B%0A                    SocketChannel socketChannel = (SocketChannel) channel;%0A                    Socket socket = socketChannel.socket();%0A                    String remoteHost = socket.getRemoteSocketAddress().toString();%0A                    logger.info(%22closing socket %7B%7D%22, remoteHost);%0A                    try %7B%0A                        socketChannel.close();%0A                    %7D catch (IOException e) %7B%0A                        logger.warn(%22Exception while closing socket%22, e);%0A                    %7D%0A                    key.cancel();%0A                %7D%0A            %7D%0A            logger.info(%22closing selector%22);%0A            selector.close();%0A        %7D catch (Exception ex) %7B%0A            logger.error(%22Exception while closing selector%22, ex);%0A        %7D%0A    %7D%0A%0A%0A    @Override%0A    public void run() %7B%0A        try %7B%0A            //%E5%BB%BA%E7%AB%8BChannel%E5%B9%B6%E7%BB%91%E5%AE%9A%E5%88%B09000%E7%AB%AF%E5%8F%A3%0A            channel = ServerSocketChannel.open().bind(new InetSocketAddress(9000));%0A            selector = Selector.open();%0A            channel.configureBlocking(false);%0A            channel.register(selector, SelectionKey.OP_ACCEPT);%0A            running = true;%0A            while (running) %7B%0A                int readyChannels = selector.select();%0A                if (readyChannels == 0) continue;%0A                Set%3CSelectionKey%3E selectedKeys = selector.selectedKeys();%0A                Iterator%3CSelectionKey%3E keyIterator = selectedKeys.iterator();%0A                while (keyIterator.hasNext()) %7B%0A                    SelectionKey key = keyIterator.next();%0A                    if (key.isAcceptable()) %7B%0A                        // a connection was accepted by a ServerSocketChannel.%0A                    %7D else if (key.isConnectable()) %7B%0A                        // a connection was established with a remote server.%0A                    %7D else if (key.isReadable()) %7B%0A                        // a channel is ready for reading%0A                    %7D else if (key.isWritable()) %7B%0A                        // a channel is ready for writing%0A                    %7D%0A                    keyIterator.remove();%0A                %7D%0A            %7D%0A        %7D catch (IOException e) %7B%0A            logger.error(e.getMessage());%0A        %7D%0A    %7D%0A%7D

Add Selector example