前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >selector与selectionKey与channel之间关系

selector与selectionKey与channel之间关系

原创
作者头像
spbreak
修改2023-09-18 08:04:09
2430
修改2023-09-18 08:04:09
举报
文章被收录于专栏:nettynetty

一. SelectionKey 简介

SelectionKey : Selector选择器注册(register()) (Channel通道+感兴趣的操作(读写...))的标记类, 用于存储(channel+操作)组合与selector之间的关系

interestOps : 通道操作的类型

OP_READ : 读

OP_WRITE : 写

OP_CONNECT : 主动连接

OP_ACCEPT : 接受接连

selectionKey内部属性
selectionKey内部属性

二. Selector与SelectionKey与Channel之间关系

Selector选择器的channelArray通道数组, 是SelectionKey(channel与selector关系映射)集合

每个selectionKey保存一个(Channel+操作事件)组合与一个Selector的关系

一个Selector可以有多个SelectionKey, 代表一个Selector可以有多个Channel, 1:N

一个Channel可以有多个SelectionKey, 代表一个Channel可以有多个Selector, 1:N

Selector与SelectionKey与Channel之间关系
Selector与SelectionKey与Channel之间关系
代码语言:javascript
复制
// 选择器类
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;
        }
    }
}

三. EventLoopGroup与EventLoop与Selector与channel之间关系

  • 一个EventLoopGroup事件循环组包含多个EventLoop事件循环
  • 一个EventLoop事件循环包含一个Selector多路复用选择器
  • 一个Selector多路复用选择器包含一组SelectionKey选择键
  • 每个selectionKey选择键关联起一个selector和一个channel文件IO操作通道
  • 每个channel对应一个文件的IO操作
EventLoopGroup与EventLoop与Selector与channel之间关系
EventLoopGroup与EventLoop与Selector与channel之间关系

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一. SelectionKey 简介
  • 二. Selector与SelectionKey与Channel之间关系
  • 三. EventLoopGroup与EventLoop与Selector与channel之间关系
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档