- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the CompletableFuture API improvements in Java 9?n
CompletableFuture API is used for asynchronous programming in Java. It means that we can write non-blocking code by running a task on a separate thread than the main() thread and notify the main() thread about its progress, completion or failure. Java 9 introduces a few improvements in CompletableFuture API, they are: "Support for timeouts and delays", "Improved support for subclassing" and "Addition of new factory methods".
Support for timeouts and delays
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)
The above method has been used to specify if the task does not complete within a certain period of time the program stops and throws TimeoutException.
public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)
The above method completes the CompletableFuture with the provided value. If not, it completes before the given timeout.
Improved support for subclassing
public Executor defaultExecutor()
The above method returns the default executor used for async methods that do not show an Executor. It may be overridden in subclasses to return an Executor to give at least one independent thread.
public <U> CompletableFuture<U> newIncompleteFuture()
The above method returns a new incomplete CompletableFuture of the specification to be returned by the CompletionStage method.
New factory Methods
public static <U> CompletableFuture<U> completedFuture(U value)
The above factory method returns a new CompletableFuture that already accomplished with the provided value.
public static <U> CompletionStage<U> completedStage(U value)
The above factory method returns a new CompletionStage that accomplished before with the provided value and is compatible with only those methods available in interface CompletionStage.
- What are the CompletableFuture API improvements in Java 9?
- What are the improvements in Process API in Java 9?
- What are the improvements for @Deprecated annotation in Java 9?
- What are the improvements for try-with-resources in Java 9?
- Differences between CompletableFuture and Future in Java 9?
- What are the steps to execute Flow API in Java 9?
- What are the core library changes in Process API in Java 9?
- When to use the delayedExecutor() method of CompletableFuture in Java 9?
- What are the new features added to Stream API in Java 9?
- What are the new methods added to Process API in Java 9?
- What is Platform Logging API in Java 9?
- StackWalker API in Java 9?
- What are the improvements in Out Parameter in C# 7.0?
- How to use the collect() method in Stream API in Java 9?
- Importance of iterate() method of Stream API in Java 9?
- How to get the parent process of the Process API in Java 9?