为了账号安全,请及时绑定邮箱和手机立即绑定

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

/ 猿问

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

binarylife 2019-09-24 23:07:47

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的添加,要放到线程中执行,而不是直接同步添加?

查看完整描述

1 回答

?
qq_496583747_0

https://github.com/netty/netty/commit/4638df20628a8987c8709f0f8e5f3679a914ce1a

谁来帮忙解释下

// We add this handler via the EventLoop as the user may have used a ChannelInitializer as handler.

// In this case the initChannel(...) method will only be called after this method returns. Because

// of this we need to ensure we add our handler in a delayed fashion so all the users handler are

// placed in front of the ServerBootstrapAcceptor.

ch.eventLoop().execute(new Runnable() {

    @Override

    public void run() {

        pipeline.addLast(new ServerBootstrapAcceptor(

currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));

    }

});


查看完整回答
反对 2019-12-17

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信