作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使 java.nio.channels.SelectionKey
对 NO opts 感兴趣?
SelectionKey#cancel()
有可能,但不太好,因为它使 key 无用。
SelectionKey
具有 interestOps
常量; OP_ACCEPT
、OP_CONNECT
、OP_READ
和 OP_WRITE
,但不是 OP_NOTHING
。那么调用SelectionKey#interestOpts(**0**)
是否合法操作?
这是一个示例。
for(;;) {
selector.select();
for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();
it.hasNext();) {
SelectionKey key = it.next(); it.remove();
key.interestOps(0); // interested in no opts.
// another thread handles socket...
worker.handle();
}
updateKeys(); // if the worker completes handling,
// other interestOpts are set...
}
到目前为止,这段代码对我来说有效,但我怀疑调用 SelectionKey#interestOpts(0)
是否合法。或者您能告诉我您的最佳实践吗?
最佳答案
为什么? Javadoc 中哪里提到了这一点?I doubt it is legal to call SelectionKey#interestOpts(0)
这是完全合法的。您已经回答了自己的问题。
关于java - NIO - 如何使 SelectionKey 对 NO opts 感兴趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17028574/
我是一名优秀的程序员,十分优秀!