Java CompletableFuture API Improvements: Enhancing Asynchronous Programming

Discover the enhancements made to the CompletableFuture class in Java 9, which builds upon the features introduced in Java 8. The CompletableFuture class represents a Future that can be completed by setting its value and status explicitly. Learn about the key improvements, including support for delays and timeouts, enhanced subclassing capabilities, and new factory methods that streamline asynchronous programming in Java.



Java - CompletableFuture API Improvements

The CompletableFuture class was introduced in Java 8 to represent a Future that can be completed by explicitly setting its value and status. It can also be used as a java.util.concurrent.CompletionStage. This class supports dependent functions and actions that get triggered upon the future's completion. In Java 9, the CompletableFuture API was further enhanced with several improvements.

Key Enhancements in CompletableFuture API

  • Support for delays and timeouts
  • Improved support for subclassing
  • New factory methods added

Support for Delays and Timeouts

The CompletableFuture API now supports timeouts, allowing futures to be completed within a specific timeframe. If the future is not completed in time, it can be either completed with a default value or with an exception.

Syntax

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

This method completes the CompletableFuture with the given value if it is not completed within the specified timeout.

Syntax

public CompletableFuture orTimeout(long timeout, TimeUnit unit)

This method exceptionally completes the CompletableFuture with a TimeoutException if it is not completed within the given timeout.

Improved Support for Subclassing

Java 9 enhances CompletableFuture by improving its support for subclassing, allowing developers to customize the behavior of asynchronous tasks and futures.

Syntax

public Executor defaultExecutor()

This method returns the default Executor used for asynchronous methods that do not specify an Executor. It can be overridden by subclasses to provide their own independent thread execution.

Syntax

public  CompletableFuture newIncompleteFuture()

This method returns a new incomplete CompletableFuture that is of the same type as the CompletionStage method that calls it. Subclasses of CompletableFuture should override this method to ensure that an instance of the same class is returned.

New Factory Methods

Java 9 introduced new factory methods to streamline the creation of completed or failed futures and stages.

Syntax

public static  CompletableFuture completedFuture(U value)

This method returns a new CompletableFuture that is already completed with the provided value.

Syntax

public static  CompletionStage completedStage(U value)

This factory method creates a CompletionStage that is already completed with the given value, supporting only the methods present in the CompletionStage interface.

Syntax

public static  CompletionStage failedStage(Throwable ex)

This method returns a CompletionStage that is exceptionally completed with the provided exception, supporting only the methods present in the CompletionStage interface.