How to use CompletableFuture in Java8
Today, the editor will share with you the relevant knowledge points about how to use CompletableFuture in Java8. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Overview
CompletableFuture is an implementation class introduced by jdk1.8. Extends Future and CompletionStage, which is a Future that triggers some operations during the task completion phase. In a nutshell, asynchronous callbacks can be implemented.
two。 Why CompletableFuture was introduced
For jdk1.5 's Future, although it provides the ability to process tasks asynchronously, the way to get the results is not elegant and still needs to be blocked (or rotational training). How to avoid congestion? It's actually a registration callback.
The industry implements asynchronous callbacks in combination with the Observer pattern. That is, to notify the observer when the mission is completed. For example, ChannelFuture of Netty can handle asynchronous results by registering and listening.
ChannelFuture of Netty
Public Promise addListener (GenericFutureListener