netty4 中 ChannelHandlerContext.executor() 和 Channel.eventLoop() 有什么区别?

What the difference between ChannelHandlerContext.executor() and Channel.eventLoop() in netty4?

netty4中ChannelHandlerContext.executor()Channel.eventLoop()有什么区别?

当我想 运行 处理程序中的任务时,我应该使用哪个?

ctx.executor().submit(task);
ctx.channel().eventLoop().submit(task);

大体上是一样的。选择什么方法由你决定。您还可以查看 AbstractChannelHandlerContext.executor() 方法并发现上下文重复频道代码:

public EventExecutor executor() {
    if (executor == null) {
        return channel().eventLoop();
    } else {
        return executor;
    }
}

但是,netty 允许您在同一管道内为不同的 EventExecutors 分配处理程序。在那种情况下,通道和上下文的返回结果可能不同。但是对于提交任务来说,这没什么大不了的。