当前位置: 首页 > news >正文

Netty — (二) 组件:EventLoop

EventLoop

EventLoop 本质是一个单线程执行器(同时维护了一个Selector),里面有run方法处理Channel上源源不断的IO事件

它的继承关系比较复杂:

  1. 一条线是继承自 java.util.concurrent.ScheduledExecutorService,因此包含了线程池中所有的方法
  2. 另一条线是继承自netty自己的 OrderedEventExecutor
    1. 提供了 boolean inEventLoop(Thread thread) 方法判断一个线程是否属于此 EventLooP
    2. 提供了 parent 方法来看看自己属于哪个 EventLoopGroup

EventLoopGroup 是一组 EventLoop,Channel 一般会调用 EventLoopGroup 的 register 方法来绑定其中一个 EventLoop,后续这个Channel 上的 IO 事件都由此 EventLoop 来处理(保证了 IO 事件处理时的线程安全)

  1. 继承自 netty 自己的 EventExecutorGroup
    1. 实现了 Iterable 接口提供遍历 EventLoop 的能力
    2. 另有 next 方法获取集合中下一个 EventLoop
@Log4j2
public class TestEventLoop {
    public static void main(String[] args) {
        // 创建事件循环组
        EventLoopGroup group = new NioEventLoopGroup(2);// IO 事件,普通任务,定时任务
//        EventLoopGroup group = new DefaultEventLoopGroup();// 普通任务,定时任务

        System.out.println(group.next());
        System.out.println(group.next());
        System.out.println(group.next());
        System.out.println(group.next());
        // 执行普通任务
//        group.next().submit(() -> {
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            log.debug("ok");
//        });

        // 定时任务
        group.next().scheduleAtFixedRate(() -> {
            log.debug("ok");
        }, 0, 1, TimeUnit.SECONDS);
        log.debug("main");
    }
}

handler 线程切换

static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
        final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
        EventExecutor executor = next.executor(); // 返回下一个handler的EventLoop
        // 是,直接调用
        if (executor.inEventLoop()) {// 当前handler中的线程,是否和eventLoop是同一个线程
            next.invokeChannelRead(m);
        } else {// 不是,将要执行的代码作为任务提交个下一个事件循环处理(换人)
            executor.execute(new Runnable() {// 下一个handler线程
                public void run() {
                    next.invokeChannelRead(m);
                }
            });
        }
    }

/**
 *如果两个handler绑定的是同一个线程,那么就直接调用,否则,把要调用的代码封装为一个任务对象,
 *由下一个handler的线程来调用
 */

上述就是EventLoop 组件的讲解,包括一些案例的实现,继续关注,后续更精彩。

相关文章:

  • Nested嵌套对象类型还挺实用
  • 这些js手写题你能回答上来几道
  • 微服务 高可用
  • TPH-yolov5 小目标检测
  • 光通信综合测试仪 光缆施工维护单位用的必要仪器--TFN HC2800
  • Putty 安装配置使用
  • Hydra复现记录(问题)
  • STM32第一课:STM硬件实物图+功能简介
  • 4. SQL语法中的一些基本概念
  • Unity注解使用方法快速上手
  • [网赚项目] 羊了个羊,周边日赚几百几千玩法
  • Appium PO模式UI自动化测试框架——设计与实践
  • Mybatis面试合集
  • 【软考 系统架构设计师】案例分析⑦ 数据库规范化与反规范化
  • Word控件Spire.Doc 【页面设置】教程(8) 如何在 C# 中创建目录 (TOC)
  • 深入了解以太坊
  • golang中接口赋值与方法集
  • iOS 系统授权开发
  • JavaScript实现分页效果
  • Laravel核心解读--Facades
  • leetcode388. Longest Absolute File Path
  • Netty 4.1 源代码学习:线程模型
  • Python实现BT种子转化为磁力链接【实战】
  • React+TypeScript入门
  • SQL 难点解决:记录的引用
  • Swoft 源码剖析 - 代码自动更新机制
  • text-decoration与color属性
  • 安装python包到指定虚拟环境
  • 分享一份非常强势的Android面试题
  • 如何解决微信端直接跳WAP端
  • 使用common-codec进行md5加密
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #if #elif #endif
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #QT(串口助手-界面)
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (3)nginx 配置(nginx.conf)
  • (C语言)fread与fwrite详解
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (八)Flask之app.route装饰器函数的参数
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (蓝桥杯每日一题)love
  • (学习日记)2024.01.19
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .“空心村”成因分析及解决对策122344
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .net core 连接数据库,通过数据库生成Modell
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • @EnableAsync和@Async开始异步任务支持