gpt4 book ai didi

java - 尝试从迭代器中删除 SelectionKey 时抛出 UnsupportedOperationException

转载 作者:行者123 更新时间:2023-12-02 02:36:27 26 4
gpt4 key购买 nike

下面的代码显示了一小块代码,它尝试接受来自客户端的传入连接(使用 java NIO 的典型实现),但是当我尝试从迭代器中删除 SelectionKey 时,它会抛出该异常。

这段代码看起来与 Jenkov tutorial 非常相似另一个 Acceptor (line 270) SocketServer from Apache Kafka .

  override def run(): Unit = {

this.logger.info("Acceptor started.")

super.run()

this.serverSocketChannel.register(this.selector, SelectionKey.OP_ACCEPT)

while (this.isRunning) {

val readyKeys = this.selector.select(500)

if (readyKeys > 0) {

val selectedKeys = this.selector.keys()

val selectionKeysIterator = selectedKeys.iterator()

while (selectionKeysIterator.hasNext && this.isRunning) {

val selectionKey = selectionKeysIterator.next()

selectionKeysIterator.remove()

if (!selectionKey.isAcceptable)
throw new IllegalStateException("The SelectionKey is not on the valid state [Acceptable].")

this.accept(selectionKey)
}
}
}

this.selector.close()
}

最佳答案

选择键集由selector.selectedKey返回(您有selector.keys,它被指定为不可修改)

关于java - 尝试从迭代器中删除 SelectionKey 时抛出 UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46244609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com