Java NIO select()返回时没有选择键-为什么? [英] Java NIO select() returns without selected keys - why?

查看:65
本文介绍了Java NIO select()返回时没有选择键-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写一些测试代码时,我发现Selector.select()可以在不包含任何要处理的键的Selector.selectedKeys()的情况下返回.当我向

In writing some test code I have found that Selector.select() can return without Selector.selectedKeys() containing any keys to process. This is happening in a tight loop when I register an accept()ed channel with

SelectionKey.OP_READ | SelectionKey.OP_CONNECT

作为感兴趣的操作.

根据文档,select()应该在以下情况下返回:

According to the docs, select() should return when:

1)有些渠道可以采取行动.

1) There are channels that can be acted upon.

2)您显式调用Selector.wakeup()-未选择任何键.

2) You explicitly call Selector.wakeup() - no keys are selected.

3)您显式地对执行select()的线程进行Thread.interrupt()-未选择任何键.

3) You explicitly Thread.interrupt() the thread doing the select() - no keys are selected.

如果在select()之后没有键,则我必须是在(2)和(3)的情况下.但是,我的代码没有调用wakeup()或interrupt()来启动这些返回.

If I get no keys after the select() I must be in cases (2) and (3). However, my code is not calling wakeup() or interrupt() to initiate these returns.

关于导致此行为的原因的任何构想?

Any ideas as to what is causing this behaviour?

推荐答案

简短答案:将OP_CONNECT从您感兴趣的可接受连接的操作列表中删除-可接受的连接已连接.

Short answer: remove OP_CONNECT from the list of operations you are interested in for the accepted connection -- an accepted connection is already connected.

我设法重现了问题,这可能恰恰是您所发生的事情:

I managed to reproduce the issue, which might be exactly what's happening to you:

import java.net.*;
import java.nio.channels.*;


public class MyNioServer {
  public static void main(String[] params) throws Exception {
    final ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.configureBlocking(true);
    serverChannel.socket().bind(new InetSocketAddress("localhost", 12345));
    System.out.println("Listening for incoming connections");
    final SocketChannel clientChannel = serverChannel.accept();
    System.out.println("Accepted connection: " + clientChannel);


    final Selector selector = Selector.open();
    clientChannel.configureBlocking(false);
    final SelectionKey clientKey = clientChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_CONNECT);
    System.out.println("Selecting...");
    System.out.println(selector.select());
    System.out.println(selector.selectedKeys().size());
    System.out.println(clientKey.readyOps());
  }
}

上述服务器接收到连接后,该连接上的第一个select()会退出而不会阻塞,并且没有可进行就绪操作的键.我不知道为什么Java会这样做,但是看来很多人都被这种行为咬了.

After the above server receives a connection, the very first select() on the connection exits without blocking and there are no keys with ready operations. I don't know why Java behaves in this way, but it appears many people get bitten by this behavior.

在Windows XP上,Sun的JVM 1.5.0_06以及在Linux 2.6上,Sun的JVM 1.5.0_05和1.4.2_04的结果是相同的.

The outcome is the same on Sun's JVM 1.5.0_06 on Windows XP as well as Sun's JVM 1.5.0_05 and 1.4.2_04 on Linux 2.6.

这篇关于Java NIO select()返回时没有选择键-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆