为什么Netty服务端启动时ServerBootstrapAcceptor的添加要放到线程中?

ServerBootstrap的init()方法中,有这样一段逻辑:

if (handler != null) {    pipeline.addLast(handler);}ch.eventLoop().execute(new Runnable() {    @Override    public void run() {	pipeline.addLast(new ServerBootstrapAcceptor(		ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));    }});

为什么对ServerBootstrapAcceptor的添加,要放到线程中执行,而不是直接同步添加?

binarylife
浏览 1288回答 5
5回答

慕移动9481536

因为还没有将 Channel 注册到 Selector 对象上,所以还无法注册 Accept 事件到 Selector 上,所以事先添加了 ChannelInitializer 处理器,等待 Channel 注册完成后,再向 Pipeline 中添加 ServerBootstrapAcceptor 处理器。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java