SelectionKey : Selector选择器注册(register()) (Channel通道+感兴趣的操作(读写...))的标记类, 用于存储(channel+操作)组合与selector之间的关系
interestOps : 通道操作的类型
OP_READ : 读
OP_WRITE : 写
OP_CONNECT : 主动连接
OP_ACCEPT : 接受接连
Selector选择器的channelArray通道数组, 是SelectionKey(channel与selector关系映射)集合
每个selectionKey保存一个(Channel+操作事件)组合与一个Selector的关系
一个Selector可以有多个SelectionKey, 代表一个Selector可以有多个Channel, 1:N
一个Channel可以有多个SelectionKey, 代表一个Channel可以有多个Selector, 1:N
// 选择器类
public abstract class SelectorImpl extends AbstractSelector {
// selector调用register注册方法
// 把channel+ops与selector保存到SelectionKey类结构里
protected final SelectionKey register(AbstractSelectableChannel channel, int ops, Object var3) {
if (!(channel instanceof SelChImpl)) {
throw new IllegalSelectorException();
} else {
// 构建selectionKey, 用channel与selector
SelectionKeyImpl selectionKey = new SelectionKeyImpl((SelChImpl)channel, this);
selectionKey.attach(var3);
synchronized(this.publicKeys) {
// selector选择器注册selectionKey, 形成关联关系
this.implRegister(selectionKey);
}
// selectionKey设置操作
selectionKey.interestOps(ops);
return selectionKey;
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有