Netty recommends using addListener to call back and forth the results of an asynchronous execution. This method is better than future. get for more precise timing of the end of an asynchronous execution.
The code is as follows:
Code running results:
Confused:Netty recommends using addListener to call back and forth the result of an asynchronous execution. This method is better than future. get, which can accurately determine when an asynchronous execution will end. It also paused for 5s. Why are they executed sequentially? Thank you
The flash, Taro, Xiaofeng’s enthusiastic answer.
This code is executed sequentially, not blocking and then finally, the REACTOR thread is synchronized.
Let’s trace the code to see:
The key here is to return a promise after executing write.
If it is reactor it is executed sequentially, otherwise it is queued for subsequent execution (sequential if it is queued, asynchronous if it is queued)
If writeAndFlush is performed asynchronously, the following methods will be performed asynchronously:
So you set the value, and then you return the promise.
So:
So this becomes the most common code in Java, a threaded code that executes from top to bottom. It also explains why there is no asynchronous case.
Netty recommends using addListener to call back and forth the results of an asynchronous execution. This method is better than futue.get for more precise timing of the end of an asynchronous execution.
The execution effect is as follows:
Continuing the analysis:
The key here is to return a promise after executing write.
If isDone is false, then addListener is added quickly and the following statement is executed.
You can also see that the writeAndFlush callback event is called after the writeAndFlush execution is complete, which is true asynchronous execution.
Thanks for the warm answers from Flash, Taro and Xiaofeng, I didn’t have a deep understanding of Netty before. Now I am much more open, and Netty continues to learn… Hope today’s article has all the harvest for you!!
Article github source code address: nettydemo, or public reply “Netty” to obtain the source code address.
END
If you feel that you have gained something after reading it, please click “like”, “follow” and add the official account “Ingenuity Zero” to check out more wonderful history!!