The CompletableFuture class in Java provides a way to write asynchronous, non-blocking code. It is a powerful feature that is part of the java.util.concurrent package, enabling developers to handle long-running operations and return the result once the operation is complete.
Java CompletableFuture Methods
The table below contains various methods of the Java CompletableFuture class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.
| Method | Description |
|---|---|
| supplyAsync() | Runs a task asynchronously and returns a CompletableFuture with the result. |
| runAsync() | Runs a task asynchronously without returning a result. |
| complete() | Completes the CompletableFuture manually with the given result. |
| completeExceptionally() | Completes the CompletableFuture manually with an exception. |
| thenApply() | Applies a function to the result of the CompletableFuture and returns a new CompletableFuture with the transformed result. |
| thenAccept() | Consumes the result of the CompletableFuture with a specified consumer without returning a result. |
| thenRun() | Runs a specified runnable after the CompletableFuture completes. |
| thenCombine() | Combines the result of this CompletableFuture with another CompletableFuture and returns a new CompletableFuture with the combined result. |
| thenAcceptBoth() | Consumes the results of both this and another CompletableFuture with a specified consumer without returning a result. |
| get() | Waits if necessary for this future to complete, and then returns its result. |
| isDone() | Returns true if this CompletableFuture is completed. |
The CompletableFuture class in Java simplifies asynchronous programming, making it easier to manage long-running tasks and handle their results efficiently.
For more comprehensive details, you can visit the official Java SE documentation.