Netty線程模型、源碼解析
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 網(wǎng)絡框架的典型模式
Mina、Netty、Cindy 都是此模式的實現(xiàn)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視圖
EventExecutorGroup里面有一個EventExecutor數(shù)組,保存了多個EventExecutor;
EventExecutorGroup是不干什么事情的,當收到一個請后,他就調(diào)用next()獲得一個它里面的EventExecutor,再調(diào)用這個executor的方法;
next(): EventExecutorChooser.next()定義選擇EventExecutor的策略;









喜歡,在看
評論
圖片
表情
