今天我们来讲讲Netty
的启动流程,要理解Netty
的启动流程,我们要理解NIO模型到Reactor模型,因为Netty的实现就是基于Reactor模型的。
一个主selector线程用来监听所有客户端的连接,然后为对应的连接上来的客户端开辟一个selector线程来监听客户端的上的事件,进行相应的处理。这就是NIO的Reactor模型。但是在Netty
中怎么实现的呢?就让我们走进Netty
的源码世界,一探究竟。由于Netty的启动流程过于复杂。我们这篇博客只讲Netty
启动流程中一小部分NioEventLoopGroup
的初始化流程。我们经常在使用Netty
的时候,会用到以下通用的代码。
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
//服务端
public class NettyServer {
public static void main(String[] args) throws InterruptedException {
//就是一个死循环,不停地检测IO事件,处理IO事件,执行任务
//创建一个线程组:接受客户端连接 主线程
EventLoopGroup bossGroup = new NioEventLoopGroup(1);//cpu核心数*2
//创建一个线程组:接受网络操作 工作线程
EventLoopGroup workerGroup = new NioEventLoopGroup(); //cpu核心数*2
//是服务端的一个启动辅助类,通过给他设置一系列参数来绑定端口启动服务
ServerBootstrap serverBootstrap = new ServerBootstrap();
// 我们需要两种类型的人干活,一个是老板,一个是工人,老板负责从外面接活,
// 接到的活分配给工人干,放到这里,bossGroup的作用就是不断地accept到新的连接,将新的连接丢给workerGroup来处理
serverBootstrap.group(bossGroup, workerGroup)
//设置使用NioServerSocketChannel作为服务器通道的实现
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128) //设置线程队列中等待连接的个数
.childOption(ChannelOption.SO_KEEPALIVE, true)//保持活动连接状态
//表示服务器启动过程中,需要经过哪些流程,这里NettyTestHandler最终的顶层接口为ChannelHandler,
// 是netty的一大核心概念,表示数据流经过的处理器
.handler(new NettyTestHendler())
//表示一条新的连接进来之后,该怎么处理,也就是上面所说的,老板如何给工人配活
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringDecoder(), new NettyServerHendler());
}
});
System.out.println(".........server init..........");
// 这里就是真正的启动过程了,绑定9090端口,等待服务器启动完毕,才会进入下行代码
ChannelFuture future = serverBootstrap.bind(9090).sync();
System.out.println(".........server start..........");
//等待服务端关闭socket
future.channel().closeFuture().sync();
// 关闭两组死循环
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
今天我们来分析一下new NioEventLoopGroup()
的过程。至于怎么编译源码,可以看我这篇博客《Idea中编译Netty源码》我们通过idea一步步看下去,首先打开的是NioEventLoopGroup
这个类,调用的代码如下:
public class NioEventLoopGroup extends MultithreadEventLoopGroup{
public NioEventLoopGroup(){
this(0);
}
public NioEventLoopGroup(int nThreads){
//nThreads = 0
//executor = null
this(nThread,(Executor)null);
}
public NioEventLoopGroup(int nThreads,Executor executor){
//nThreads = 0
//executor = null
//selectorProvider = SelectorProvider.provider()
this(nThread,executor,SelectorProvider.provider());
}
public NioEventLoopGroup(int nThreads,Executor executor,final SelectorProvider selectorProvider){
//nThreads = 0
//executor = null
//selectorProvider = SelectorProvider.provider()
//selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE = new DefaultSelectStrategyFactory() 默认选择策略工厂
this(nThread,executor,selectorProvider,DefaultSelectStrategyFactory.INSTANCE);
}
public NioEventLoopGroup(int nThreads,Executor executor,
final SelectorProvider selectorProvider,
final SelectStrategyFactory selectStrategyFactory){
//nThreads = 0
//executor = null
//selectorProvider = SelectorProvider.provider()
//selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE => new DefaultSelectStrategyFactory() 默认选择策略工厂
//RejectedExecutionHandlers.reject() => new RejectedExecutionHandler() => 丢弃任务并抛出RejectedExecutionException异常
super(nThread,executor,selectorProvider,selectStrategyFactory,RejectedExecutionHandlers.reject());
}
}
可以看到创建的过程就是一连串的构造函数的调用,最终传入的参数如下
# 最终创建的selector线程的数量
nThreads = 0
# 执行器
executor = null
# 一个工具类可以打开Nio的一些常用通道,用于后面打开ServerSocketChannel
selectorProvider = SelectorProvider.provider()
# 默认选择策略工厂
selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE => new DefaultSelectStrategyFactory()
# 线程池的拒绝策略线程池满的时候丢弃任务并抛出RejectedExecutionException异常
RejectedExecutionHandlers.reject() => new RejectedExecutionHandler()
SelectorProvider.provider()
的方法如下图所示
由于上面的调用链,调用了父类的方法,我们继续打开NioEventLoopGroup
的父类MultithreadEventLoopGroup
类,继续看调用链
public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup{
private static final int DEFAULT_EVENT_LOOP_THREADS;
static{
DEFAULT_EVENT_LOOP_THREADS = Math.max(1,SystemPropertyUtil.getInt(
"io.netty.eventLoopThreads",NettyRuntime.availableProcessors() *2))
}
protected MultithreadEventLoopGroup(int nThreads,Executor executor, Object ... args){
//nThreads如果不传是0,如果是0的话,先获取配置中io.netty.eventLoopThreads,如果没有直接获取CPU核心线程数的两倍
//executor = null
//args{
//selectorProvider = SelectorProvider.provider()
//selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE => new DefaultSelectStrategyFactory() 默认选择策略工厂
//RejectedExecutionHandlers.reject() => new RejectedExecutionHandler() => 丢弃任务并抛出RejectedExecutionException异常
//}
super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS:nthreads,executor,args)
}
}
我们来写个类测试一下,是不是获取CPU核心线程数的两倍,具体运行结果如下:
由于笔者使用的MacBook Pro 16寸的i7版,是6核12线程,所以这儿是24。
最终传入的参数如下:
nThreads如果不传是0,如果是0的话,先获取配置中io.netty.eventLoopThreads,如果没有直接获取CPU核心线程数的两倍
executor = null
args{
selectorProvider = SelectorProvider.provider()
selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE => new DefaultSelectStrategyFactory() 默认选择策略工厂
RejectedExecutionHandlers.reject() => new RejectedExecutionHandler() => 丢弃任务并抛出RejectedExecutionException异常
}
可以看到上面的调用链,又调用到了MultithreadEventLoopGroup
的父类MultithreadEventExecutorGroup
,具体的代码如下:
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup{
protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object ... args){
//nThreads如果不传是0,如果是0的话,先获取配置中io.netty.eventLoopThreads,如果没有直接获取CPU核心线程数的两倍
//笔者这儿是24
//executor = null
//DefaultEventExecutorChooserFactory.INSTANCE 默认的事件的执行选择器工厂
//args{
//selectorProvider = SelectorProvider.provider()
//selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE => new DefaultSelectStrategyFactory() 默认选择策略工厂
//RejectedExecutionHandlers.reject() => new RejectedExecutionHandler() => 丢弃任务并抛出RejectedExecutionException异常
//}
this(nThreads, executor,DefaultEventExecutorChooserFactory.INSTANCE, args);
}
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args){
if(nThreads <= 0){
throw new IllegalArgumentExecption(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if(executor == null){
//先调用newDefaultThreadFactory()方法
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
children = new EventExecutor[nThreads];
//....未完待续
}
}
我们再去查看newDefaultThreadFactory()
方法,由于从MultithreadEventLoopGroup
调用过来的,而MultithreadEventLoopGroup
重写父类MultithreadEventExecutorGroup
的newDefaultThreadFactory
的方法,具体如下:
public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup{
protected ThreadFactory newDefaultThreadFactory(){
//getClass() = NioEventLoopGroup.class
//Thread.MAX_PRIORITY=10
return new DefaultThreadFactory(getClass(), Thread.MAX_PRIORITY);
}
}
我们继续看调用链,这儿创建的是默认的线程工厂DefaultThreadFactory
,具体的代码如下:
public class DefaultThreadFactory implements ThreadFactory{
public DefaultThreadFactory(Class<?> poolType, int priority){
//poolType = NioEventLoopGroup.class
//daemon = false 不是守护线程
//priority = 10 线程可以拥有的最大优先级 10
this(poolType, false, priority);
}
public DefaultThreadFactory(Class<?> poolType, boolean daemon, int priority){
//toPoolName(poolType) = nioEventLoopGroup
//daemon = false 不是守护线程
//priority = 10 线程可以拥有的最大优先级 10
this(toPoolName(poolType), daemon, priority);
}
//根据类类型(class) 获取类名 并且将首字母转为小写
public static String toPoolName(Class<?> poolType){
if(poolType == null){
throw new NullPointerExection("poolType");
}
//获取类名
String poolName = StringUtil.simpleClassName(poolType);
switch(poolName.length()){
case 0:
return "unknown";
case 1:
//将大写字母转为小写
return poolName.toLawerCase(Locale.US);
default:
if(Character.isUpperCase(poolName.charAt(0)) && Character.isLowerCase(poolName.charAt(1))){
return Character.toLowerCase(poolName.charAt(0)) + poolName.substring(1);
}else{
return poolName;
}
}
}
public DefaultThreadFactory(String poolName, boolean daemon, int priority){
//poolName = nioEventLoopGroup
//daemon = false 不是守护线程
//priority = 线程可以拥有的最大优先级 10
//System.getSecurityManager() == null 判断有没有安全管理器
this(poolName, daemon, priority, System.getSecurityManager() == null ?
Thread.currentThread.getThreadGroup :
System.getSecurityManager().getThreadGroup());
}
public DefaultThreadFactory(String poolName, boolean daemon, int priority, ThreadGroup threadGroup) {
// poolName = nioEventLoopGroup
// daemon = false 是否是守护进程,后台程序
// priority ==> 线程可以拥有的最大优先级。 10
// threadGroup 线程组
if (poolName == null) {
throw new NullPointerException("poolName");
}
if (priority < Thread.MIN_PRIORITY || priority > Thread.MAX_PRIORITY) {
throw new IllegalArgumentException(
"priority: " + priority + " (expected: Thread.MIN_PRIORITY<=priority<=Thread.MAX_PRIORITY)");
}
//prefix=nioEventLoopGroup-(原子计数器++)-
prefix = poolName + '-' + poolId.incrementAndGet() + '-';
//false
this.daemon = daemon;
//10
this.priority = priority;
//线程组
this.threadGroup = threadGroup;
}
}
上面的代码的一步步调用,最后给DefaultThreadFactory
进行对应的赋值,最后DefaultThreadFactory
如下图所示
最终将创建好的DefaultThreadFactory
返回到MultithreadEventExecutorGroup
中对应的方法中去,具体的代码如下:
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup{
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args){
if(nThreads <= 0){
throw new IllegalArgumentExecption(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if(executor == null){
//返回到如下的代码中,继续创建ThreadPerTaskExecutor
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
children = new EventExecutor[nThreads];
//....未完待续
}
}
new ThreadPerTaskExecutor(newDefaultThreadFactory())
,将创建好的DefaultThreadFactory()
赋值给ThreadPerTaskExecutor
代码会调用如下的代码:
/**
* Executor是一个顶层接口,在它里面只声明了一个方法execute(Runnable),返回值为void,
* 参数为Runnable类型,从字面意思可以理解,就是用来执行传进去的任务的;
*/
public final class ThreadPerTaskExecutor implements Executor {
//DefaultThreadFactory
private final ThreadFactory threadFactory;
public ThreadPerTaskExecutor(ThreadFactory threadFactory) {
if (threadFactory == null) {
throw new NullPointerException("threadFactory");
}
this.threadFactory = threadFactory;
}
//我们要执行开启一个线程时,只需要调用此方法传一个Runnable任务 此方法就会通过threadFactory创建一个线程去执行
@Override
public void execute(Runnable command) {
//new DefaultThreadFactory()
threadFactory.newThread(command).start();
}
}
最终的ThreadPerTaskExecutor
类的如下图所示:
上面的代码执行完,executor
就等于上图的值了。接下来我们看我们还没有读的代码,具体的代码如下:
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup{
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args){
if(nThreads <= 0){
throw new IllegalArgumentExecption(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if(executor == null){
//执行完,executor等于上图的ThreadPerTaskExecutor,
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
//创建对应的EventExecutor数组。数组的长度为nThreads 如果不传是0,如果是0的话,先获取配置中io.netty.eventLoopThreads,如果没有直接获取CPU核心线程数的两倍 笔者这儿是24
children = new EventExecutor[nThreads];
for (int i = 0; i < nThreads; i ++) {
//出现异常标识
boolean success = false;
try {
//创建nThreads个nioEventLoop保存到children数组中
//接下来要看的循环创建的过程。
children[i] = newChild(executor, args);
success = true;
} catch (Exception e) {
// TODO: Think about if this is a good exception type
throw new IllegalStateException("failed to create a child event loop", e);
} finally {
//出现异常处理
if (!success) {
for (int j = 0; j < i; j ++) {
children[j].shutdownGracefully();
}
for (int j = 0; j < i; j ++) {
EventExecutor e = children[j];
try {
while (!e.isTerminated()) {
e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
}
} catch (InterruptedException interrupted) {
// Let the caller handle the interruption.
Thread.currentThread().interrupt();
break;
}
}
}
}
}
//....未完待续
}
}
接下来我们要看的就是newChild(executor, args);
的代码,传入的参数如下:
# 具体的格式如上面画的图
executor = ThreadPerTaskExecutor
arg{
selectorProvider = SelectorProvider.provider()
# 默认选择策略工厂
selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE => new DefaultSelectStrategyFactory()
# 线程池任务满的时候 丢弃任务并抛出RejectedExecutionException异常
RejectedExecutionHandlers.reject() => new RejectedExecutionHandler()
}
调用的代码如下:
public class NioEventLoopGroup extends MultithreadEventLoopGroup {
@Override
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
return new NioEventLoop(this, executor, (SelectorProvider) args[0],
((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}
}
可以看到上面的代码创建了一个NioEventLoop
最后返回给我们的children
,我们再来看看创建的NioEventLoop
的过程。具体的代码如下:
传入的参数如下:
# 具体的格式如上面画的图
executor = ThreadPerTaskExecutor
selectorProvider = SelectorProvider.provider()
# 默认选择策略工厂
selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE => new DefaultSelectStrategyFactory()
# 线程池任务满的时候 丢弃任务并抛出RejectedExecutionException异常
RejectedExecutionHandlers.reject() => new RejectedExecutionHandler()
public final class NioEventLoop extends SingleThreadEventLoop {
NioEventLoop(NioEventLoopGroup parent, Executor executor,
SelectorProvider selectorProvider,
SelectStrategy strategy,
RejectedExecutionHandler rejectedExecutionHandler) {
//首先调用的是父类的构造函数,我们先先查看父类的构造函数。
super(parent, executor, false,
DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
if (selectorProvider == null) {
throw new NullPointerException("selectorProvider");
}
if (strategy == null) {
throw new NullPointerException("selectStrategy");
}
//provider=SelectorProvider.provider()
provider = selectorProvider;
final SelectorTuple selectorTuple = openSelector();
//替换了数据结构selectedKeys publicSelectedKeys的原生selector
selector = selectorTuple.selector;
//子类包装的selector 底层数据结构也是被替换了的
unwrappedSelector = selectorTuple.unwrappedSelector;
//selectStrategy = new DefaultSelectStrategyFactory()
selectStrategy = strategy;
}
}
调用父类的SingleThreadEventLoop
的构造函数如下所示:
public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop {
//int 最大值,要不就是自己配置的值,如果自己配置的小于16,那么这个值就是16.
protected static final int DEFAULT_MAX_PENDING_TASKS = Math.max(16,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxPendingTasks", Integer.MAX_VALUE));
protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedExecutionHandler) {
//NioEventLoopGroup.this
//executor=new ThreadPerTaskExecutor(newDefaultThreadFactory());
//addTaskWakesUp=false
//maxPendingTasks=2147483647 默认最大挂起任务
//rejectedExecutionHandler ===> new RejectedExecutionHandler()
//又是调用父类的构造函数
super(parent, executor, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
//return maxPendingTasks == Integer.MAX_VALUE ? PlatformDependent.<Runnable>newMpscQueue()
//: PlatformDependent.<Runnable>newMpscQueue(maxPendingTasks);
tailTasks = newTaskQueue(maxPendingTasks);
}
}
调用父类的SingleThreadEventExecutor
的构造函数如下所示:
public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedHandler) {
//parent = NioEventLoopGroup.this
//executor = new ThreadPerTaskExecutor(new DefaultThreadFactory());
//addTaskWakesUp=false
//maxPendingTasks=2147483647
//rejectedExecutionHandler ===> new RejectedExecutionHandler()
//调用父类,最后是最顶级的父类中保留这个值,我们在这儿就不再继续跟进去了,如果你有兴趣的话,可以继续跟进去看看
super(parent);
// addTaskWakesUp=false
this.addTaskWakesUp = addTaskWakesUp;
//2147483647
this.maxPendingTasks = Math.max(16, maxPendingTasks);
//executor=执行器
this.executor = ThreadExecutorMap.apply(executor, this);
//return maxPendingTasks == Integer.MAX_VALUE ? PlatformDependent.<Runnable>newMpscQueue()
//: PlatformDependent.<Runnable>newMpscQueue(maxPendingTasks);
taskQueue = newTaskQueue(this.maxPendingTasks);
//new RejectedExecutionHandler()
rejectedExecutionHandler = ObjectUtil.checkNotNull(rejectedHandler, "rejectedHandler");
}
}
通过上面的代码,我们又进行新的一轮的赋值,具体的结果如下图所示:
上面代码执行完SingleThreadEventExecutor
就变成上图的样子。这个时候会返回到SingleThreadEventLoop
类中执行相应的代码,具体的代码如下:
public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop {
protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedExecutionHandler) {
//NioEventLoopGroup.this
//executor=new ThreadPerTaskExecutor(newDefaultThreadFactory());
// addTaskWakesUp=false
//maxPendingTasks=2147483647 默认最大挂起任务
//rejectedExecutionHandler ===> new RejectedExecutionHandler()
//调用的结果返回。然后给当前类SingleThreadEventLoop中tailTasks进行相应的赋值。
super(parent, executor, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
//return maxPendingTasks == Integer.MAX_VALUE ? PlatformDependent.<Runnable>newMpscQueue()
//: PlatformDependent.<Runnable>newMpscQueue(maxPendingTasks);
tailTasks = newTaskQueue(maxPendingTasks);
}
}
在SingleThreadEventLoop
赋值后,最后返回到NioEventLoop
继续执行构造函数,具体代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
NioEventLoop(NioEventLoopGroup parent, Executor executor,
SelectorProvider selectorProvider,
SelectStrategy strategy,
RejectedExecutionHandler rejectedExecutionHandler) {
//调用父类的方法返回,继续执行下面的方法
super(parent, executor, false,
DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
if (selectorProvider == null) {
throw new NullPointerException("selectorProvider");
}
if (strategy == null) {
throw new NullPointerException("selectStrategy");
}
//provider=SelectorProvider.provider()
provider = selectorProvider;
//首先执行下面的代码
final SelectorTuple selectorTuple = openSelector();
//替换了数据结构selectedKeys publicSelectedKeys的原生selector
selector = selectorTuple.selector;
//子类包装的selector 底层数据结构也是被替换了的
unwrappedSelector = selectorTuple.unwrappedSelector;
//selectStrategy = new DefaultSelectStrategyFactory()
selectStrategy = strategy;
}
}
先是调用openSelector();
方法,我们再看看对应的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
private static final class SelectorTuple {
//子类包装的selector
final Selector unwrappedSelector;
//替换了数据结构selectedKeys
final Selector selector;
SelectorTuple(Selector unwrappedSelector) {
this.unwrappedSelector = unwrappedSelector;
this.selector = unwrappedSelector;
}
SelectorTuple(Selector unwrappedSelector, Selector selector) {
this.unwrappedSelector = unwrappedSelector;
this.selector = selector;
}
}
//创建selector 并且将selector中的selectedKeys publicSelectedKeys 从set集合用数组替代
private SelectorTuple openSelector() {
final Selector unwrappedSelector;
try {
//调用nio api创建selector 这儿创建
unwrappedSelector = provider.openSelector();
} catch (IOException e) {
throw new ChannelException("failed to open a new selector", e);
}
if (DISABLE_KEY_SET_OPTIMIZATION) {
//将selector保存到SelectorTuple
return new SelectorTuple(unwrappedSelector);
}
//加载SelectorImpl类 得到SelectorImpl的类类型
Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
return Class.forName(
"sun.nio.ch.SelectorImpl",
false,
PlatformDependent.getSystemClassLoader());
} catch (Throwable cause) {
return cause;
}
}
});
//isAssignableFrom()方法是从类继承的角度去判断,instanceof关键字是从实例继承的角度去判断。
//isAssignableFrom()方法是判断是否为某个类的父类,instanceof关键字是判断是否某个类的子类。
//使用方法 父类.class.isAssignableFrom(子类.class) 子类实例 instanceof 父类类型
if (!(maybeSelectorImplClass instanceof Class) ||
!((Class<?>) maybeSelectorImplClass).isAssignableFrom(unwrappedSelector.getClass())) {
if (maybeSelectorImplClass instanceof Throwable) {
Throwable t = (Throwable) maybeSelectorImplClass;
logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, t);
}
return new SelectorTuple(unwrappedSelector);
}
final Class<?> selectorImplClass = (Class<?>) maybeSelectorImplClass;
//netty自定义的Set
final SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
Object maybeException = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
//获取原生的selector中两个属性
Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
//如果jdk的版本大于等于9的时候,执行下面的代码
if (PlatformDependent.javaVersion() >= 9 && PlatformDependent.hasUnsafe()) {
long selectedKeysFieldOffset =
PlatformDependent.objectFieldOffset(selectedKeysField);
long publicSelectedKeysFieldOffset=
PlatformDependent.objectFieldOffset(publicSelectedKeysField);
if (selectedKeysFieldOffset != -1 && publicSelectedKeysFieldOffset != -1) {
PlatformDependent.putObject(
unwrappedSelector, selectedKeysFieldOffset, selectedKeySet);
PlatformDependent.putObject(
unwrappedSelector, publicSelectedKeysFieldOffset, selectedKeySet);
return null;
}
}
//将原来的属性改成public的
Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField, true);
if (cause != null) {
return cause;
}
//将原来的属性改成public的
cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField, true);
if (cause != null) {
return cause;
}
//替换SelectorImpl中原先使用的Set实现类
selectedKeysField.set(unwrappedSelector, selectedKeySet);
publicSelectedKeysField.set(unwrappedSelector, selectedKeySet);
return null;
} catch (NoSuchFieldException e) {
return e;
} catch (IllegalAccessException e) {
return e;
}
}
});
if (maybeException instanceof Exception) {
selectedKeys = null;
Exception e = (Exception) maybeException;
logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, e);
return new SelectorTuple(unwrappedSelector);
}
//将自定义的selectedKey保存起来
selectedKeys = selectedKeySet;
logger.trace("instrumented a special java.util.Set into: {}", unwrappedSelector);
//unwrappedSelector 原生的selector但是内部的东西已经被替换了
return new SelectorTuple(unwrappedSelector,
new SelectedSelectionKeySetSelector(unwrappedSelector, selectedKeySet));
}
}
上面的方法就是替换了原生的selector
中selectedKeys
和publicSelectedKeys
的两个属性为netty
自定义的selectedKeys
,同时将这个selector
进行包装,对其中一些方法进行一些前置的处理,有点类似动态代理,但是又不是。执行openSelector()
返回,继续执行赋值的操作,具体的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
NioEventLoop(NioEventLoopGroup parent, Executor executor,
SelectorProvider selectorProvider,
SelectStrategy strategy,
RejectedExecutionHandler rejectedExecutionHandler) {
super(parent, executor, false,
DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
if (selectorProvider == null) {
throw new NullPointerException("selectorProvider");
}
if (strategy == null) {
throw new NullPointerException("selectStrategy");
}
//provider=SelectorProvider.provider()
provider = selectorProvider;
//执行下面的代码返回,继续执行下面的赋值的操作
final SelectorTuple selectorTuple = openSelector();
//替换了数据结构selectedKeys publicSelectedKeys的原生selector
selector = selectorTuple.selector;
//子类包装的selector 底层数据结构也是被替换了的
unwrappedSelector = selectorTuple.unwrappedSelector;
//selectStrategy = new DefaultSelectStrategyFactory()
selectStrategy = strategy;
}
}
赋值完的NioEventLoop
类如下图所示:
至此循环创建children数组就完成。我们在返回原来的代码继续往下执行。具体代码如下:
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup{
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args){
if(nThreads <= 0){
throw new IllegalArgumentExecption(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if(executor == null){
//执行完,executor等于上图的ThreadPerTaskExecutor,
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
//创建对应的EventExecutor数组。数组的长度为nThreads 如果不传是0,如果是0的话,先获取配置中io.netty.eventLoopThreads,如果没有直接获取CPU核心线程数的两倍 笔者这儿是24
children = new EventExecutor[nThreads];
for (int i = 0; i < nThreads; i ++) {
//出现异常标识
boolean success = false;
try {
//创建nThreads个nioEventLoop保存到children数组中
//循环创建NioEventLoop,然后赋值到数组中去。
children[i] = newChild(executor, args);
success = true;
} catch (Exception e) {
// TODO: Think about if this is a good exception type
throw new IllegalStateException("failed to create a child event loop", e);
} finally {
//出现异常处理
if (!success) {
for (int j = 0; j < i; j ++) {
children[j].shutdownGracefully();
}
for (int j = 0; j < i; j ++) {
EventExecutor e = children[j];
try {
while (!e.isTerminated()) {
e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
}
} catch (InterruptedException interrupted) {
// Let the caller handle the interruption.
Thread.currentThread().interrupt();
break;
}
}
}
}
}
//children是 new NioEventLoop() 的对象数组
//chooser=GenericEventExecutorChooser/PowerOfTwoEventExecutorChooser 事件执行策略工厂
//这个时候执行下面的代码,事件执行的策略工厂,决定使用那个NioEventLoop执行相应的代码
chooser = chooserFactory.newChooser(children);
final FutureListener<Object> terminationListener = new FutureListener<Object>() {
@Override
public void operationComplete(Future<Object> future) throws Exception {
if (terminatedChildren.incrementAndGet() == children.length) {
terminationFuture.setSuccess(null);
}
}
};
for (EventExecutor e: children) {
e.terminationFuture().addListener(terminationListener);
}
//复制一份children 只读的
Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
Collections.addAll(childrenSet, children);
readonlyChildren = Collections.unmodifiableSet(childrenSet);
}
}
上面的循环创建NioEventLoop
结束后,我们去执行对应的创建的事件执行策略工厂,具体代码如下:
/**
* 使用简单的循环选择next的默认实现
*/
@UnstableApi
public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory {
public static final DefaultEventExecutorChooserFactory INSTANCE = new DefaultEventExecutorChooserFactory();
private DefaultEventExecutorChooserFactory() { }
@SuppressWarnings("unchecked")
@Override
public EventExecutorChooser newChooser(EventExecutor[] executors) {
// executors是 new NioEventLoop() 的对象数组
if (isPowerOfTwo(executors.length)) {
return new PowerOfTwoEventExecutorChooser(executors);
} else {
return new GenericEventExecutorChooser(executors);
}
}
//判断幂等性 2.4.8.16
private static boolean isPowerOfTwo(int val) {
return (val & -val) == val;
}
private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {
// executors是 new NioEventLoop() 的对象数组
this.executors = executors;
}
//从executors对象数组中返回new NioEventLoop()对象
@Override
public EventExecutor next() {
return executors[idx.getAndIncrement() & executors.length - 1];
}
}
private static final class GenericEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
GenericEventExecutorChooser(EventExecutor[] executors) {
// executors是 new NioEventLoop() 的对象数组
this.executors = executors;
}
//从executors对象数组中返回new NioEventLoop()对象
@Override
public EventExecutor next() {
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}
}
}
上面的代码表示事件执行策略工厂有两种方式,分别对应的不同的数组的长度,如果数组的长度是2的指数倍(包括1),这个时候会选择PowerOfTwoEventExecutorChooser
策略。如果不是会选择GenericEventExecutorChooser
策略模式,两种策略模式的取法是一样,都是顺序取。事件执行的策略工厂执行完,继续执行下面的代码:
public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup{
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args){
if(nThreads <= 0){
throw new IllegalArgumentExecption(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if(executor == null){
//执行完,executor等于上图的ThreadPerTaskExecutor,
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
//创建对应的EventExecutor数组。数组的长度为nThreads 如果不传是0,如果是0的话,先获取配置中io.netty.eventLoopThreads,如果没有直接获取CPU核心线程数的两倍 笔者这儿是24
children = new EventExecutor[nThreads];
for (int i = 0; i < nThreads; i ++) {
//出现异常标识
boolean success = false;
try {
//创建nThreads个nioEventLoop保存到children数组中
//循环创建NioEventLoop,然后赋值到数组中去。
children[i] = newChild(executor, args);
success = true;
} catch (Exception e) {
// TODO: Think about if this is a good exception type
throw new IllegalStateException("failed to create a child event loop", e);
} finally {
//出现异常处理
if (!success) {
for (int j = 0; j < i; j ++) {
children[j].shutdownGracefully();
}
for (int j = 0; j < i; j ++) {
EventExecutor e = children[j];
try {
while (!e.isTerminated()) {
e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
}
} catch (InterruptedException interrupted) {
// Let the caller handle the interruption.
Thread.currentThread().interrupt();
break;
}
}
}
}
}
//children是 new NioEventLoop() 的对象数组
//chooser=GenericEventExecutorChooser/PowerOfTwoEventExecutorChooser 事件执行策略工厂
//这段代码执行完返回,会继续执行下面的代码
chooser = chooserFactory.newChooser(children);
//创建对应的线程监听器
final FutureListener<Object> terminationListener = new FutureListener<Object>() {
@Override
//线程执行完成的时候执行的代码
public void operationComplete(Future<Object> future) throws Exception {
if (terminatedChildren.incrementAndGet() == children.length) {
terminationFuture.setSuccess(null);
}
}
};
//将所有创建好NioEventLoop添加上面的监听器
for (EventExecutor e: children) {
e.terminationFuture().addListener(terminationListener);
}
//复制一份children 只读的
Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
Collections.addAll(childrenSet, children);
readonlyChildren = Collections.unmodifiableSet(childrenSet);
}
}
最终执行完,对应的MultithreadEventExecutorGroup
如下图所示:
到此new NioEventLoopGroup()
执行结束,大致流程讲完了,就是给上面我画的几个对象进行对应赋值。