2021SC@SDUSC
在绑定端口过程中,类initAndRegister里有注册通道方法ChannelFuture regFuture = config().group().register(channel);
首先会发现
public ChannelFuture register(Channel channel) {
return next().register(channel);
}
这里先分析next()
public EventLoop next() {
return (EventLoop) super.next();
}
public EventExecutor next() {
return chooser.next();
}
public EventExecutor next() {
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}
最后他return的是一个EventExecutor 执行器,是一个workgroup里的某个子线程。然后转成了EventLoop对象
然后就是EventLoop对象的.register(channel)方法
public ChannelFuture register(Channel channel) {
return register(new DefaultChannelPromise(channel, this));
}
public ChannelFuture register(ChannelPromise promise) {
ObjectUtil.checkNotNull(promise, "promise");
promise.channel().unsafe().register(this, promise);
return promise;
}
将channel和自己EventLoop封装成DefaultChannelPromise对象后又调用了promise.channel().unsafe().register(this, promise);方法,这里的.channel().unsafe()都分别返回了channel和unsafe,关键在register
public final void register(EventLoop eventLoop, final ChannelPromise promise) {
if (eventLoop == null) {
throw new NullPointerException("eventLoop")</