Netty inEventLoop method? Asynchronous callback?

Article Directory


inEventLoop

Has not been very understanding of this method, it is known that thread is to determine the current thread is not corresponding in the current EventLoop in?
A channel corresponds to one and only one corresponds EventLoop, a Channel corresponds to one and only one for Pipeline, Pipline contains handler (also context), the current Channel Handler is invoked, it is executed when the code of the corresponding thread is not the channel that EventLoop a thread?

EventExecutor most critical is inEventLoop method for judging whether a thread is an internal thread that EventExecutor, EventExecutor and EventLoop are single-threaded implementation. inEventLoop main usage scenario is that when the IO changes, pipeline through the channel associated with the trigger event corresponding to the callback method pipeline in the processing chain handler of these events corresponding to execution, 每个handler添加到pipeline都可以指定自己的EventLoop,如果没指定,默认使用要添加的pipeline关联的channel注册到的EventLoopGroup中的某个EventLoop。so the channel through the pipeline call handler, if the handler does not designated EventLoop alone, that inEventLoop returns true, it made the same thread processing, direct call handler. If the handler is specified separately EventLoop, inEventLoop will return false, method invocation handler when the channel to be called to put the package to Runnable, and then add to the handler specified EventLoop task queue, it will be made later in the thread corresponding EventLoop carried out.

From: https://www.jianshu.com/p/d39ff4c98c5f

As for code analysis code is not posted, nothing special, the key is to analyze the situation.

Himself drew a diagram to facilitate understanding, of course, there must have been wrong places, but understanding is not very good, there are problems wish to make progress together!
Here Insert Picture Description
So there are two issues the following
Here Insert Picture Description

In the handler:
? Executor.inEventLoop ()
current executor is the corresponding eventLoop channel, and inEventLoop in currentThread handler is customized eventLoop, it is not equal. So is false, false when the callback handler packaged into their corresponding task into the task queue eventLoop

This is asynchronous

The current channel is equivalent to the main thread, the main thread callback want to find the current handler has its own processing thread, then put the package into the callback method to his own task Executor in the queue, he himself to perform it, I why why go.

Supplements


What is asynchronous?
Do you want to wait for the completion of an operation?

What is blocking?
Do you want to wait for a data ready?


Of course, in Netty There is also a more interesting:

WriteAndFlush and addListener method, if it is asynchronous, before writeAndFlush, completed AddListener, then executing the abnormal in WriteAndFlush write callback, or callback when done to flush operationComplete is provided for Success, a callback can be assured.
But if the two are synchronized execution method if, of course, also be executed asynchronously, writeAndFlush execution finished, result is set to Success, but AddListener has not added, it is up to how the callback? operationComplete not ready yet. By looking at the source code to know notifyListenersNow when the first judgment listeners (in fact, listener) is not no, there is no direct return, that does not mean the wirteAnfFlush event callback? In fact, AddListener method, will conduct a judgment as to the current result is not Success, yes, it would be a pullback.
Code Analysis slightly.


There ByteBuf cache hit and we should feel for.


Disclaimer:
I own a small white or only see the tip of the iceberg, may not be entirely correct, if there is a serious mistake in the text, we wish to point out as soon as possible, not only conducive to their correction, but also reduce misunderstanding others for knowledge.

Published 554 original articles · won praise 3996 · Views 2.83 million +

Guess you like

Origin blog.csdn.net/dataiyangu/article/details/105357431