CompletableFuture: Introduction
This lesson introduces the newly added CompletableFuture interface.
Introduction to CompletableFuture
interface
A CompletableFuture
is a class in Java that belongs to the java.util.concurrent
package.
It is used for asynchronous computation. The code is executed as a non-blocking call in a separate thread, and the result is made available when it is ready.
By doing this, the main thread does not block/wait for the completion of the task, and it can execute other tasks in parallel.
The CompletableFuture
class implements the CompletionStage
and Future
interface. The CompletionStage
is a promise. It promises that the computation eventually will be done.
Before Java 8, Future
interface, which was added in Java 1.5, was available for asynchronous computation. The limitation of Future
interface is that it does not have any methods to combine these computations or handle errors. We will address more limitations of Future interface in the next section.
CompletableFuture
has lots of different methods for composing, combining, executing asynchronous computation steps, and handling errors.
Get hands-on with 1400+ tech skills courses.