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

标签 java scala unsupportedoperation

下面的代码显示了一小块代码,它尝试接受来自客户端的传入连接(使用 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/

相关文章:

java - 在处理 "long"的大值时,这段代码有什么问题?

java - 线程 "main"java.lang.RuntimeException : Uncompilable source code 中的异常

scala - 扩展教程:java.lang.ClassNotFoundException

JavaFX 播放音频时出现问题 : Unsupported protocol "file"

java - AspectJ-结合方法注释和设置字段

scala - scala 字节码中不必要的加载和存储指令

scala - 结构化scala案例类的自定义json序列化

java.lang.UnsupportedOperationException : 'posix:permissions' not supported as initial attribute on Windows

java.lang.UnsupportedOperationException : 'posix:permissions' not supported as initial attribute on Windows 异常

java - 程序进入无限循环-Java