我正在阅读Netty 4.0.10.Final的源代码。 在AbstractChannel.AbstractUnsafe类
中 private void invokeLater(Runnable task) {
// This method is used by outbound operation implementations to trigger an inbound event later.
// They do not trigger an inbound event immediately because an outbound operation might have been
// triggered by another inbound event handler method. If fired immediately, the call stack
// will look like this for example:
//
// handlerA.inboundBufferUpdated() - (1) an inbound handler method closes a connection.
// -> handlerA.ctx.close()
// -> channel.unsafe.close()
// -> handlerA.channelInactive() - (2) another inbound handler method called while in (1) yet
//
// which means the execution of two inbound handler methods of the same handler overlap undesirably.
eventLoop().execute(task);
}
有评论困惑我。 为什么出站事件会立即触发入站事件。 有人可以帮我解释一下细节吗?谢谢!
答案 0 :(得分:0)
(我已经回答了这个问题,但我正在回答这个问题。)
评论所说的基本上我们想确保同一处理程序中处理程序方法的执行不重叠。如果没有invokeLater()
,即使在handlerA.channelInactive()
返回之前也会调用handlerA.inboundBufferUpdated()
。