加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Selector.close()是否关闭所有客户端套接字?

发布时间:2020-12-15 04:20:56 所属栏目:Java 来源:网络整理
导读:我是nio套接字的新手,我使用nio套接字编写了一个服务器,现在我正在尝试编写shutdown hook以确保通过清理资源来优雅退出. 我的问题是Selector.close()方法关闭所有客户端套接字?如果没有,请告诉我如何访问所有客户端套接字,而无需单独列出它们. Java Doc说跟
我是nio套接字的新手,我使用nio套接字编写了一个服务器,现在我正在尝试编写shutdown hook以确保通过清理资源来优雅退出.

我的问题是Selector.close()方法关闭所有客户端套接字?如果没有,请告诉我如何访问所有客户端套接字,而无需单独列出它们.

Java Doc说跟随selector.close()方法

Closes this selector.

If a thread is currently blocked in one of this
selector’s selection methods then it is interrupted as if by invoking
the selector’s wakeup method.

Any uncancelled keys still associated with this selector are
invalidated,their channels are deregistered,and any other resources
associated with this selector are released.

If this selector is already closed then invoking this method has no
effect.

After a selector is closed,any further attempt to use it,except by
invoking this method or the wakeup method,will cause a
ClosedSelectorException to be thrown.

上面的描述使用了“deregistered”这个词,它给人的感觉是它不会关闭套接字而只是从选择器中删除它们的映射.

解决方法

不,它只关闭选择器.

在关闭选择器之前,可以通过Selector.keys()访问所有已注册的套接字密钥.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读