java - 无法在单独的线程中设置 SelectionKey 的interestOps

标签 java multithreading nio

我正在尝试使用带有可选 channel 的 NIO 创建一个简单的服务器,并将主 NIO 循环之外的所有“重”逻辑移至单独的线程中。但我无法从其他线程注册 SelectionKey 。抱歉读得太长了。

服务器照常启动:

ServerSocketChannel serverChannel;
Selector selector;
try {
   serverChannel = ServerSocketChannel.open();
   ServerSocket ss = serverChannel.socket();
   InetSocketAddress address = new InetSocketAddress(port);
   ss.bind(address);
   serverChannel.configureBlocking(false);
   selector = Selector.open();
   serverChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (IOException ex) {
   ex.printStackTrace();
   return;
}

然后进入主循环,在接受阶段(key.isAcceptable())我执行接受(我更喜欢在单独的线程中接受连接,但似乎如果没有主NIO循环中的接受,我将无法得到SocketChannel 对象):

ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel sChann = server.accept();

然后我将当前的 SocketChannel 和 SelectionKey 传递给第二个线程,以便进行一些检查并决定是否应该关闭 channel 或者我可以从 channel 读取数据。如果所有检查均成功通过,我尝试为此 key 注册 OP_READ 标志并遇到以下问题:

Java 手册中写道,SelectionKey 对于 channel 来说是常量。但是,当在第二个线程中时,我尝试执行

key.interestOps(SelectionKey.OP_READ);

我遇到以下异常:

Exception in thread "Thread-0" java.lang.IllegalArgumentException
    at java.base/sun.nio.ch.SelectionKeyImpl.interestOps(SelectionKeyImpl.java:98)
    at ConnectionAcceptor.run(ConnectionAcceptor.java:55)
    at java.base/java.lang.Thread.run(Thread.java:834)

我在手册中读到了有关此异常的信息

IllegalArgumentException - If a bit in the set does not correspond to an operation that is supported by this key's channel, that is, if (ops & ~channel().validOps()) != 0

并添加了一些检查以查看是否是我的情况。第二个线程中的检查是:

System.out.println("ConnectionAcceptor: valid options " + ci.sockChan.validOps());
System.out.println("ConnectionAcceptor: OP_ACCEPT " + SelectionKey.OP_ACCEPT);
System.out.println("ConnectionAcceptor: OP_READ " + SelectionKey.OP_READ);
System.out.println("ConnectionAcceptor: OP_WRITE " + SelectionKey.OP_WRITE);

结果是:

ConnectionAcceptor: valid options 13
ConnectionAcceptor: OP_ACCEPT 16
ConnectionAcceptor: OP_READ 1
ConnectionAcceptor: OP_WRITE 4

因此,没有违反手册中的规则,并且不应引发 IllegalArgumentException。

Here我找到了另一种设置所需标志的方法:

sockChan.keyFor(selector).interestOps(SelectionKey.OP_READ);

但是在我的第二个线程中使用它我得到了

Exception in thread "Thread-0" java.lang.NullPointerException
    at ConnectionAcceptor.run(ConnectionAcceptor.java:59)
    at java.base/java.lang.Thread.run(Thread.java:834)

因此,看起来当 channel 和关键对象被传输到第二个线程时,主 NIO 循环进行了一些迭代,并且 channel 的 SelectionKey 变得无效。 请帮我找到从第二个线程注册 channel 选择器标志的方法。

最佳答案

key.interestOps(SelectionKey.OP_READ);

您正在尝试更改注册的兴趣集

/* SelectionKey key = */ serverChannel.register(selector, SelectionKey.OP_ACCEPT);

ServerSocketChannel仅支持OP_ACCEPT

您想要做的是在新接受的套接字 channel 上注册一个新的OP_READ

SelectionKey aNewKey = sChann.register(selector, SelectionKey.OP_READ);

该新 key 将驱动该套接字上的操作。

<小时/>

您的第二次尝试失败,因为您的 SocketChannel 未注册任何 Selector,因此 keyFor 返回 null .

关于java - 无法在单独的线程中设置 SelectionKey 的interestOps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61344608/

相关文章:

java - 如何使用 java-selenium 中的 cucumber bdd 框架处理 Bootstrap 下拉列表

java - 使用 Twitter4j 从服务器端反向验证 token

java - 为什么 Java 不断消耗更多内存?

c++ - 从 Qt 中的多个线程绘制

Python 记录集中的多线程

java - 观看目录时仅触发一次事件

java - 从 ByteBuffer 创建 DataBufferUShort

java - Cucumber 场景断言 DataTable 中的条件

multithreading - 这是比赛条件吗?

java - RandomAccessFile 与 FileChannel.open(path);