0

私はJavaのノンブロッキングIOに不慣れです。質問があります-チャネルからの読み取りが完了した後、セレクターからこのチャネルの選択キーを削除する前にサーバーからの新しいパケットが到着した場合、非ブロッキングチャネルの準備はセレクターによって失われますか?ここにサンプルコード:

        Selector selector;

        // ......

        while (true) {
            selector.select();
            Set<SelectionKey> set = selector.selectedKeys();

            Iterator<SelectionKey> iterator = set.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                SocketChannel channel = (SocketChannel) key.channel();
                ByteBuffer byteBuffer = ByteBuffer.allocate(GOOD_ENOUGH_CAPACITY);
                while (channel.read(byteBuffer) > 0) ;

                // HERE ! What happen if server started to write new message here?
                // Will this channel be selected on next selector.select() ?

                iterator.remove();

            }
        }
4

1 に答える 1

1

はい、キーが選択されます。メソッドを使用する必要があります

key.cancel(); 

セレクターからキーを削除するには

于 2012-04-21T18:30:54.540 に答える