- Java 9 Tutorial
- Java 9 - Home
- Java 9 - Overview
- Java 9 - Environment Setup
- Java 9 - Module System
- Java 9 - REPL (JShell)
- Java 9 - Improved JavaDocs
- Java 9 - Multirelease JAR
- Java 9 - Collection Factory Methods
- Java 9 - Private Interface Methods
- Java 9 - Process API Improvements
- Java 9 - Stream API Improvements
- Try With Resources improvement
- Enhanced @Deprecated Annotation
- Inner Class Diamond Operator
- Optional Class Improvements
- Java 9 - Multiresolution Image API
- CompletableFuture API Improvements
- Java 9 - Miscellaneous Features
- java9 Useful Resources
- Java 9 - Questions and Answers
- Java 9 - Quick Guide
- Java 9 - Useful Resources
- Java 9 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
CompletableFuture API Improvements
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.
- Support for delays and timeouts.
- Improved support for subclassing.
- New factory methods added.
Support for delays and timeouts
public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)
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)
This method exceptionally completes this CompletableFuture with a TimeoutException if not otherwise completed before the given timeout.
Improved support for subclassing
public Executor defaultExecutor()
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()
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)
This factory method returns a new CompletableFuture which is already completed with the given value.
public static <U> CompletionStage<U> completedStage(U value)
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)
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.