关于netty的EventLoop

简介: 关于netty的EventLoop

什么是EventLoop

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

什么是EventLoopGroup

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

NioEventLoopGroup eventExecutors = new NioEventLoopGroup();

NioEventLoopGroup默认线程数为当前CPU核心数*2;如果有传参则以传参为准。

NioEventLoopGroup支持IO事件,普通任务事件,定时任务事件。

packagenetty.c2;
importio.netty.channel.nio.NioEventLoopGroup;
importjavafx.scene.layout.GridPane;
importjava.util.concurrent.TimeUnit;
publicclassTestEventLoop {
publicstaticvoidmain(String[] args) {
NioEventLoopGroupeventExecutors=newNioEventLoopGroup(2);
System.out.println(eventExecutors.next());
System.out.println(eventExecutors.next());
System.out.println(eventExecutors.next());
System.out.println(eventExecutors.next());
// 执行普通任务//        eventExecutors.next().submit(() -> {//            try {//                System.out.println("thread");//                Thread.sleep(1000);//            } catch (InterruptedException e) {//                e.printStackTrace();//            }//        });//        System.out.println("main");// 执行定时任务eventExecutors.next().scheduleAtFixedRate(() -> {
try {
System.out.println("thread");
Thread.sleep(1000);
            } catch (InterruptedExceptione) {
e.printStackTrace();
            }
        }, 0, 1, TimeUnit.SECONDS);
    }
}

eventLoop和channel进行了绑定。

image.png

考虑一个问题:

如果一个work可以绑定多个channel。那么如果某个channel上的handler执行过久,就直接影响work上其他线程的执行时间,怎么处理?

解法:创建一个独立的EventLoopGroup。channel.pipeline().addLast(新的group, "xxx", new ChannelInboundHandlerAdapter(){})。要注意handler的顺序。

image.png

image.png

剖析:如果两个handler绑定的是同一个线程,那么就直接调用;否则,把要调用的代码封装为一个任务对象,由下一个handler线程来调用(切换线程)。

相关文章
|
9月前
|
缓存 Java 测试技术
Netty实战(七)EventLoop和线程模型
简单地说,线程模型指定了操作系统、编程语言、框架或者应用程序的上下文中的线程管理的关键方面。
98 0
|
9月前
|
监控 安全 Java
Netty之EventLoop 解读
Netty之EventLoop 解读
|
Java API 调度
Netty组件EventLoopGroup和EventLoop源码分析
Netty组件EventLoopGroup和EventLoop源码分析
51 0
|
安全 Java
Netty「源码阅读」之 EventLoop 简单介绍到源码分析
Netty「源码阅读」之 EventLoop 简单介绍到源码分析
171 0
|
存储 前端开发
netty系列之:EventLoop,EventLoopGroup和netty的默认实现
netty系列之:EventLoop,EventLoopGroup和netty的默认实现
netty系列之:EventLoop,EventLoopGroup和netty的默认实现
|
缓存 Java 测试技术
【Netty】EventLoop和线程模型
在学习了ChannelHandler和ChannelPipeline的有关细节后,接着学习Netty的EventLoop和线程模型。
173 0
【Netty】EventLoop和线程模型
|
缓存 算法 Java
Netty4的EventLoop和线程模型原理解析
线程模型指定了os、编程语言、框架或应用程序的上下文中的线程管理的关键方面。如何、何时创建线程将对应用程序代码执行产生显著影响,开发人员必须理解不同模型之间的权衡。 而 Netty 的线程模型强大又易用,正如 Netty 的宗旨:简化你的应用程序代码,同时最大限度提高性能和可维护性。
146 0
Netty4的EventLoop和线程模型原理解析
|
Java API 调度
Netty4 实战精华EventLoop 和线程模型
简单地说,线程模型指定了操作系统、编程语言、框架或者应用程序的上下文中的线程管理的关键方面。 显而易见地,如何以及何时创建线程将对应用程序代码的执行产生显著的影响,因此开发人员需要理解与不同模型相关的权衡。
1400 0
Netty In Action中文版 - 第十六章:从EventLoop取消注册和重新注册
Netty In Action中文版 - 第十六章:从EventLoop取消注册和重新注册 本章介绍 EventLoop 从EventLoop注册和取消注册 在Netty中使用旧的Socket和Channel Netty提供了一个简单的方法来连接Socket/Channel,这是在Netty之外创建并转移他们的责任到Netty。
1548 0
|
安全 网络协议 Java
Netty原理篇-EventLoop、EventLoopGroup
Netty的线程模型 Netty Reactor线程模型源码分析 Netty使用的最佳实践
2733 0