gpt4 book ai didi

java - NIO - 如何使 SelectionKey 对 NO opts 感兴趣

转载 作者:行者123 更新时间:2023-12-02 06:58:15 24 4
gpt4 key购买 nike

如何使 java.nio.channels.SelectionKey 对 NO opts 感兴趣?

SelectionKey#cancel() 有可能,但不太好,因为它使 key 无用。

SelectionKey 具有 interestOps 常量; OP_ACCEPTOP_CONNECTOP_READOP_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) 是否合法。或者您能告诉我您的最佳实践吗?

最佳答案

I doubt it is legal to call SelectionKey#interestOpts(0)

为什么? Javadoc 中哪里提到了这一点?

这是完全合法的。您已经回答了自己的问题。

关于java - NIO - 如何使 SelectionKey 对 NO opts 感兴趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17028574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com