Java 简明教程

Java - CompletableFuture API Improvements

Java 8 中引入了 CompletableFuture 类,用于表示可以显式设置其值和状态的 Future。它可用作 java.util.concurrent.CompletionStage。它支持在 future 完成时触发的依赖函数和操作。在 java 9 CompletableFuture 中进一步增强了 API。以下是针对 API 做出的相关更改。

CompletableFuture class was introduced in Java 8 to represent the Future which can be completed by setting its value and status explicity. It can be used as java.util.concurrent.CompletionStage. It supports dependent functions and actions which got triggered upon the future’s completion. In java 9 CompletableFuture API has been enhanced further. Following are the relevant changes done to the API.

  1. Support for delays and timeouts.

  2. Improved support for subclassing.

  3. New factory methods added.

Support for delays and timeouts

public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)

如果在给定超时之前未完成,则此方法使用给定值完成此 CompletableFuture。

This method completes this CompletableFuture with the given value if not otherwise completed before the given timeout.

public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)

如果在给定超时之前尚未完成,则此方法使用 TimeoutException 异常完成此 CompletableFuture。

This method exceptionally completes this CompletableFuture with a TimeoutException if not otherwise completed before the given timeout.

Improved support for subclassing

public Executor defaultExecutor()

它返回用于没有指定 Executor 的异步方法的默认 Executor。此方法可以在子类中被重写,以至少返回一个用于提供一个独立线程的 Executor。

It returns the default Executor used for async methods that do not specify an Executor. This method may be overridden in subclasses to return an Executor to provide one independent thread as minimum.

public <U> CompletableFuture<U> newIncompleteFuture()

返回由 CompletionStage 方法返回的类型的新的未完成 CompletableFuture。CompletableFuture 类的子类应重写此方法,以返回与此 CompletableFuture 相同类的实例。默认实现返回 CompletableFuture 类的实例。

Returns a new incomplete CompletableFuture of the type to be returned by a CompletionStage method. Subclasses of CompletableFuture class should override this method to return an instance of the same class as this CompletableFuture. The default implementation returns an instance of class CompletableFuture.

New factory Methods

public static <U> CompletableFuture<U> completedFuture(U value)

此工厂方法返回一个新的 CompletableFuture,该 CompletableFuture 已使用给定值完成。

This factory method returns a new CompletableFuture which is already completed with the given value.

public static <U> CompletionStage<U> completedStage(U value)

此工厂方法返回一个新的 CompletionStage,该 CompletionStage 已使用给定值完成,并且仅支持接口 CompletionStage 中存在的方法。

This factory method returns a new CompletionStage which is already completed with the given value and supports only those methods present in interface CompletionStage.

public static <U> CompletionStage<U> failedStage(Throwable ex)

此工厂方法返回一个新的 CompletionStage,该 CompletionStage 已使用给定异常完成,并且仅支持接口 CompletionStage 中存在的方法。

This factory method returns a new CompletionStage which is already completed exceptionally with the given exception and supports only those methods present in interface CompletionStage.