Netty线程模型、源码解析

JAVA乐园

共 1750字,需浏览 4分钟

 · 2021-06-13

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的策略;

喜欢,在看


浏览 20
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报