Netty线程模型、源码解析

本文介绍了Reactor模式在Java NIO网络框架中的应用,如Mina、Netty和Cindy的实现,讨论了单线程和多线程EventExecutorGroup的不同配置,以及如何通过EventExecutor进行事件处理。重点讲解了ServerBootstrap中EventLoopGroup的选择和使用策略。

0x01:Reactor 模式

Douglas C. Schmidt 1995年提出:

An Object Behavioral Pattern for Demultiplexing and 
Dispatching Handles for Synchronous Events

Scalable IO in Java - Doug Lea

http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf
基于Java IO对Reactor模式进行阐述

NIO 网络框架的典型模式

Mina、Netty、Cindy 都是此模式的实现
  • Douglas

  • Doug Lea三种模式

单线程

多线程 Reactor

Multiple Reactor

其他主从形式,但是不常用

Netty 和 Reactor 的模式同时都支持的

  • 单线程的Reactor

EventLoopGroup eventExecutors = new NioEventLoopGroup(1);
ServerBootstrap serverBootstrap = new ServerBootstrap().group(eventExecutors,eventExecutors);

  • 多线程

EventLoopGroup eventExecutors = new NioEventLoopGroup(1);
ServerBootstrap serverBootstrap = new ServerBootstrap().group(eventExecutors,eventExecutors);//Handler使用线程池进行处理

Multiple Reactor

EventLoopGroup eventExecutors = new NioEventLoopGroup(1);
EventLoopGroup workereventExecutors = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap().group(eventExecutors,workereventExecutors);

EventLoopGroup eventExecutors = new NioEventLoopGroup();
EventLoopGroup workereventExecutors = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap().group(eventExecutors,workereventExecutors);

0x02: 源码

EventExecutor视图

  1. EventExecutorGroup里面有一个EventExecutor数组,保存了多个EventExecutor;

  2. EventExecutorGroup是不干什么事情的,当收到一个请后,他就调用next()获得一个它里面的EventExecutor,再调用这个executor的方法;

  3. next(): EventExecutorChooser.next()定义选择EventExecutor的策略;

喜欢,在看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值