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模式進(jìn)行闡述NIO 網(wǎng)絡(luò)框架的典型模式
Mina、Netty、Cindy 都是此模式的實(shí)現(xiàn)Douglas

Doug Lea三種模式
單線程

多線程 Reactor

Multiple Reactor

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

Netty 和 Reactor 的模式同時(shí)都支持的

單線程的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使用線程池進(jìn)行處理

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里面有一個(gè)EventExecutor數(shù)組,保存了多個(gè)EventExecutor;
EventExecutorGroup是不干什么事情的,當(dāng)收到一個(gè)請后,他就調(diào)用next()獲得一個(gè)它里面的EventExecutor,再調(diào)用這個(gè)executor的方法;
next(): EventExecutorChooser.next()定義選擇EventExecutor的策略;









喜歡,在看
評論
圖片
表情
