我是 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();
}
}