Java NIO注册时的一个疑问

对相同的通道注册不同的相关事件返回同一个SelectionKey,但其特性又不一样,这是怎么实现的?

public class Server {

    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress("localhost", 7777));
        serverSocketChannel.configureBlocking(false);

        Selector selector = Selector.open();

        SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        boolean run = true;
        while (run == true) {
            int keyCount = selector.select();
            Set<SelectionKey> set1 = selector.keys();
            Set<SelectionKey> set2 = selector.selectedKeys();
            System.out.println("keyCountA =" + keyCount);
            System.out.println("set1 size=" + set1.size());
            System.out.println("set2 size=" + set2.size());
            System.out.println();
            Iterator<SelectionKey> iterator = set2.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                if (key.isAcceptable()) {
                    ServerSocketChannel channel = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = channel.accept();
                    socketChannel.configureBlocking(false);
                    SelectionKey key1 = socketChannel.register(selector, SelectionKey.OP_READ);
                    System.out.println("key1.isReadable()=" + ((SelectionKey.OP_READ & ~key1.interestOps()) == 0));
                    System.out.println("key1.isWritable()=" + ((SelectionKey.OP_WRITE & ~key1.interestOps()) == 0));

                    SelectionKey key2 = socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
                    System.out.println("key2.isReadable()=" + ((SelectionKey.OP_READ & ~key2.interestOps()) == 0));
                    System.out.println("key2.isWritable()=" + ((SelectionKey.OP_WRITE & ~key2.interestOps()) == 0));

                    System.out.println("keyCountB =" + keyCount);
                    System.out.println("set1 size=" + set1.size());
                    System.out.println("set2 size=" + set2.size());
                    System.out.println("key1==key2结果:" + (key1 == key2));
                }
                iterator.remove();
            }
            TimeUnit.HOURS.sleep(1);
        }
        selector.close();
    }
}
public class Client {

    public static void main(String[] args) throws IOException, InterruptedException {
        Socket socket = new Socket("localhost", 7777);
        socket.getOutputStream().write("12345".getBytes());
        TimeUnit.HOURS.sleep(1);
    }
keyCountA =1
set1 size=1
set2 size=1

key1.isReadable()=true
key1.isWritable()=false
key2.isReadable()=true
key2.isWritable()=true
keyCountB =1
set1 size=2
set2 size=1
key1==key2结果:true
阅读 191
评论
    0 个回答
    撰写回答

    登录后参与交流、获取后续更新提醒

    相似问题
    推荐文章