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.

Completable future is used as future

Execute code asynchronously

Combined futures

The difference between thenapply() and thenpose()

Parallel execution of tasks

exception handling

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>