For everything about completable future, it’s enough to read this article
In the previous article, we explained future. In this article, we will continue to explain the usage of completable future introduced in Java 8.
Completable future is first of all a future. It has all the functions of future, including obtaining asynchronous execution results, canceling executing tasks, etc.
In addition, completable future is also a completionstage.
Let's look at the definition of completable future:
public class CompletableFuture<T> implements Future<T>, CompletionStage<T>
What is completionstage?
In asynchronous programs, if each asynchronous execution is regarded as a stage, it is usually difficult for us to control the execution order of asynchronous programs. In JavaScript, we need to execute callbacks in callbacks. This will form the legendary hell.
Fortunately, the concept of promise is introduced in ES6, which can convert the callback in the callback into a chain call, which greatly improves the readability and writability of the program.
Similarly, in Java, we use completionstage to realize the chain operation of asynchronous call.
Completionstage defines a series of then * * * operations to achieve this function.