Home

CompletableFuture

CompletableFuture is a class in the Java standard library that represents a future result of an asynchronous computation. It implements the Future and CompletionStage interfaces, enabling both blocking retrieval of a result and non-blocking composition of dependent tasks. A CompletableFuture can be completed explicitly by code calling complete or completeExceptionally, or it can be completed automatically when a submitted asynchronous task finishes.

A key feature is its support for composing asynchronous stages without blocking threads. The API provides a

Factory methods include supplyAsync and runAsync to start tasks asynchronously, as well as completedFuture to create

CompletableFuture is designed to be thread-safe and reusable in concurrent programming. It serves as a bridge

rich
set
of
methods
for
transforming,
consuming,
and
combining
results,
such
as
thenApply,
thenAccept,
thenRun,
thenCompose,
and
handle.
These
methods
return
new
CompletableFuture
instances,
enabling
fluent
pipelines
where
each
stage
starts
when
its
predecessor
completes.
Exceptions
can
be
propagated
and
handled
with
exceptionally
and
handle.
an
already-completed
stage.
Streams
of
futures
can
be
coordinated
with
allOf
and
anyOf,
which
complete
when
their
component
futures
complete.
If
no
executor
is
supplied,
asynchronous
tasks
default
to
the
common
ForkJoinPool,
but
a
custom
Executor
can
be
provided
to
control
threading.
between
traditional
blocking
futures
and
a
more
flexible,
non-blocking
style
of
asynchronous
computation,
allowing
developers
to
express
complex
workflows
as
a
pipeline
of
dependent
stages
rather
than
nested
callbacks.