老师我想和您讨论下关于NioEventLoop的启动时机问题

来源:4-6 NioEventLoop的启动

ice_wolf

2020-03-08

您在课程里说在是在doBind0()方法里调用了eventLoop的execute方法使得NioEventLoop启动的,但是我发现在执行将服务端Channel注册到Selector的时候经过了如下过程:

@Override
public final void register(EventLoop eventLoop, final ChannelPromise promise) {
    if (eventLoop == null) {
        throw new NullPointerException("eventLoop");
    }
    if (isRegistered()) {
        promise.setFailure(new IllegalStateException("registered to an event loop already"));
        return;
    }
    if (!isCompatible(eventLoop)) {
        promise.setFailure(
                new IllegalStateException("incompatible event loop type: " + eventLoop.getClass().getName()));
        return;
    }

    AbstractChannel.this.eventLoop = eventLoop;

    if (eventLoop.inEventLoop()) {
        register0(promise);
    } else {
        try {
	        //这里也调用了execute()方法
            eventLoop.execute(new Runnable() {
                @Override
                public void run() {
                    register0(promise);
                }
            });
        } catch (Throwable t) {
            logger.warn(
                    "Force-closing a channel whose registration task was not accepted by an event loop: {}",
                    AbstractChannel.this, t);
            closeForcibly();
            closeFuture.setClosed();
            safeSetFailure(promise, t);
        }
    }
}

上面代码中也调用了EventLoop的exectue()方法,而且这个调用时间比绑定端口早,为啥不是在这里启动的NioEventLoop?但是想想有点奇怪,如果是在这里那都还没绑定端口好像说不通啊,请老师解惑。

写回答

1回答

闪电侠

2020-03-10

我看你后面发了问题说搞懂了,如果还有问题,可以重新提哈

0
0

Java读源码之Netty深入剖析

解析netty各大组件细节,百万级性能调优,设计模式实际运用

2334 学习 · 283 问题

查看课程