English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java 8 Introduces the CompletableFuture<T> class, which may be the explicit completion version of java.util.concurrent.Future<T> (with its value and state set), or may be used as java.util.concurrent.CompleteStage. Supports triggering some dependent functions and actions when the future is completed. Java 9 SomeCompletableFuture are introduced:
Java 9 Improvements to CompletableFuture Improvements made:
Supports delays and timeouts
Enhances support for subclassing
New factory method
public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)
in timeoutUnits in java.util.concurrent.Timeunits units in, such as MILLISECONDS), completes this CompletableFutrue with the given value. Returns this CompletableFutrue.
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)
If the CompletableFutrue is not completed within the given timeout, complete this CompletableFutrue with java.util.concurrent.TimeoutException, and return this CompletableFutrue.
has made many improvements to CompletableFuture can be inherited more simply. For example, you might want to override the new public Executor defaultExecutor() method to replace the default executor.
Another new method that makes subclassing easier is:
public <U> CompletableFuture<U> newIncompleteFuture()
Java 8Introduces the <U> CompletableFuture<U> completedFuture(U value) factory method to return a CompletableFuture that has already been completed with the given value. Java 9supplements this method with a new <U> CompletableFuture<U> failedFuture(Throwable ex) that can return a CompletableFuture that completes with the given exception.
In addition to this, Java 9 introduced the following pair of stages-oriented factory methods, returning completed or exceptionally completed completion stages:
<U> CompletionStage<U> completedStage(U value): Returns a new CompletionStage that completes with the specified value, and only supports the interfaces within CompletionStage
<U> CompletionStage<U> failedStage(Throwable ex): Returns a new CompletionStage that completes with the specified exceptionCompletionStage , and only supports CompletionStage interfaces.