netty源码解解析(4.0)-6 线程模型-IO线程EventLoopGroup和NIO实现(一)
接口定义
io.netty.channel.EventLoopGroup extends EventExecutorGroup
方法
说明
ChannelFuture register(Channel channel)
把一个channel注册到一个EventLoop
ChannelFuture register(Channel channel, ChannelPromise promise);
同上
io.netty.channel.EventLoop extends OrderedEventExecutor, EventLoopGroup
方法
说明
EventLoopGroup parent()
得到创建这个eventLoop的EventLoopGroup
EventLoopGroup定义的主要方法是register, 这个方法的语义是把channel和eventLoop绑定在一起。一个channel对应一个eventLoop, 一个eventLoop会持有多个channel。
I/O线程EventLoopGroup的抽象实现
io.netty.channel.MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup
io.netty.channel.SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop
两个类主功能都是实现了EventLoopGroup定义的register方法
MultithreadEventLoopGroup
public ChannelFuture register(Channel channel) {
return next().register(channel);
}
public ChannelFuture register(Channel channel, ChannelPromise promise) {
return next().register(channel, promise);
}
SingleThreadEventLoop
public ChannelFuture register(Channel channel) {
return register(channel, new DefaultChannelPromise(channel, this));
}
public ChannelFuture register(final Channel channel, final ChannelPromise promise) {
channel.unsafe().register(this, promise);
return promise;
}
register的实现主要是为了调用Channel.Unsafe实例的register方法。
NIO实现
io.netty.channel.nio.NioEventLoopGroup extends MultithreadEventLoopGroup
io.netty.channel.nio.NioEventLoop extends SingleThreadEventLoop
NioEventLoopGroup是在MultithreadEventLoopGroup基础上实现了对JDK NIO Selector的封装, 它实现以下几个功能:
创建selector
在selector上注册channel感兴趣的NIO事件
实现EventExecutor的run方法,定义NIO事件和Executor任务的处理流程。
把NIO事件转换成对channel unsafe的调用或NioTask的调用
控制线程执行I/O操作和排队任务的用时比例
处理epoll selector cpu 100%的bug
下面来具体分析这几个功能的实现。
创建Selector
NioEventLoop#openSelector()实现了创建selector的功能,默认情况下,使用SelectorProvider#openSelector()方法创建一个新个selector:
final Selector unwrappedSelector = provider.openSelector();
如果设置环境变量io.netty.noKeySetOptimization=true, 会创建一个selectedKeySet = new SelectedSelectionKeySet(), 然后使用java的反射机制把selector的selectedKeys和publicSelectedKeys替换成selectedKeySet,具体步骤是:
1.得到selector的真正类型: sun.nio.ch.SelectorImpl
Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction