netty源码阅读之EventLoop实例化过程
继续EventloopGroup的实例化创建过程中给该对象分配多个EventLoop对象的时候会调用到如下方法
@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构造器
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;
selector = openSelector();
selectStrategy = strategy;
}
因为它调用了父类的构造函数,所以得一路追下去看它干了啥,在追下去的时候看一下它的继承关系
SingleThreadEventLoop类
看这个类的所有方法名字与注释,其主要的功能就是注册channel与运行任务。
构造器的代码
protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedExecutionHandler) {
super(parent, executor, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
tailTasks = newTaskQueue(maxPendingTasks);
}
SingleThreadEventExecutor类
继续向下追看到的构造器代码如下,根据整个类的代码注释与类名可以说它才是任务的执行器。
protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedHandler) {
super(parent);
this.addTaskWakesUp = addTaskWakesUp;
this.maxPendingTasks = Math.max(16, maxPendingTasks);
this.executor = ObjectUtil.checkNotNull(executor, "executor");
taskQueue = newTaskQueue(this.maxPendingTasks);
rejectedExecutionHandler = ObjectUtil.checkNotNull(rejectedHandler, "rejectedHandler");
}
它保存了executor的引用,已经确定了任务队列的大小。
AbstractScheduledEventExecutor类
该处理器就是用于处理定时调度的任务的处理器,也拥有自己的任务队列。
public abstract class AbstractScheduledEventExecutor extends AbstractEventExecutor {
Queue<ScheduledFutureTask<?>> scheduledTaskQueue;
protected AbstractScheduledEventExecutor() {
}
protected AbstractScheduledEventExecutor(EventExecutorGroup parent) {
super(parent);
}
AbstractEventExecutor类
执行到这里没有再调用父类的构造器了,那它就干了这件事情–将EventExecutorGroup,与将当前的EventLoop进行保存,目前不知道干啥用
public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractEventExecutor.class);
static final long DEFAULT_SHUTDOWN_QUIET_PERIOD = 2;
static final long DEFAULT_SHUTDOWN_TIMEOUT = 15;
private final EventExecutorGroup parent;
private final Collection<EventExecutor> selfCollection = Collections.<EventExecutor>singleton(this);
protected AbstractEventExecutor() {
this(null);
}
protected AbstractEventExecutor(EventExecutorGroup parent) {
this.parent = parent;
}
再回SingleThreadEventExecutor类
他开始初始化各个成员变量了,而且存放要执行的任务是MpscChunkedArrayQueue队列,因为newTaskQueue被子类NioEventLoop重写了
@Override
protected Queue<Runnable> newTaskQueue(int maxPendingTasks) {
// This event loop never calls takeTask()
return PlatformDependent.newMpscQueue(maxPendingTasks);
}
再回SingleThreadEventLoop类
tailTasks = newTaskQueue(maxPendingTasks);
这个类又在创建一个队列,目前不清楚为啥一个eventloop为啥要有两个队列。它们之间的区别又是在哪,留在后面探究。/(ㄒoㄒ)/~~
再回到NioEventLoop类
开始获取 selector,以及创建用于保存selectedKey的集合,对selector进行优化。
provider = selectorProvider;
selector = openSelector();
selectStrategy = strategy;
final SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
到此一个NioEventLoop完成完整的创建过程~~~~