Java NIO select()返回时没有选择键

编程入门 行业动态 更新时间:2024-10-28 01:15:46
本文介绍了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.*; 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()返回时没有选择键

本文发布于:2023-10-16 22:13:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   NIO   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!