Java CompletableFuture Methods

The CompletableFuture class in Java is part of the java.util.concurrent package and provides a powerful way to handle asynchronous computations. This class allows you to create complex asynchronous pipelines by combining multiple futures. The CompletableFuture class supports various methods for running tasks asynchronously and handling their results when they become available.

This guide covers various methods available in the CompletableFuture class. Each method is described in simple terms to help beginners understand how to use them. These methods enable you to run tasks asynchronously, handle their results, and chain multiple asynchronous tasks together.

For more detailed information, please refer to the official Java SE Documentation.

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 future with a given value.
completeExceptionally() Completes the future with an exception.
thenApply() Transforms the result of the future when it becomes available.
thenAccept() Consumes the result of the future when it becomes available.
thenRun() Runs a task after the future is complete, without using its result.
thenCombine() Combines the results of two futures when both complete.
thenAcceptBoth() Consumes the results of two futures when both complete.
get() Waits for the future to complete and retrieves its result.
isDone() Checks if the future is complete.

Comments