1.

CompletableFuture API enhancements in Java 9

Answer»

In Java 9, the CompletableFuture API has been further developed. Some of the changes done to the API are:

  • Support for timeouts and delays
  • Improved support for subclassing
  • Addition of new factory methods

Support for timeouts and delays

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

This METHOD COMPLETES the CompletableFuture with the provided value. If not, it completes it before the GIVEN timeout.

Improved support for subclassing

public Executor DEFAULTEXECUTOR()

It returns the default Executor used for async methods that do not show an Executor. This method may be OVERRIDDEN in subclasses to return an Executor to give at least one independent thread

public <U> CompletableFuture<U> newIncompleteFuture()

It returns a new incomplete CompletableFuture of the specification to be returned by a CompletionStage method.

New factory Methods

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

This factory method returns a new CompletableFuture which is already accomplished with the provided value.

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

This factory method returns a new CompletionStage which is accomplished beforehand with the provided value and is compatible with only those methods available in interface CompletionStage.



Discussion

No Comment Found