Home

CompletableFuturecompletedFuturedone

CompletableFuture.completedFuture is a static factory method in the Java API for CompletableFuture. It returns a new CompletableFuture that is already completed with the given non-null value, allowing immediate use in asynchronous pipelines without creating a new asynchronous task.

Behavior and semantics: The returned future is in a completed state from the moment of creation. If

Usage and examples: A common pattern is to adapt APIs that return futures or to provide a

Null handling and related methods: Because the value must be non-null, care should be taken to ensure

Overall, completedFuture offers a simple way to produce a ready, immutable result that participates in CompletableFuture-based

you
attach
dependent
stages
such
as
thenApply,
thenAccept,
or
whenComplete,
those
actions
may
execute
immediately
in
the
calling
thread,
since
there
is
no
pending
computation
to
perform.
If
you
need
asynchronous
execution
for
the
subsequent
stages,
you
can
use
the
asynchronous
variants
like
thenApplyAsync
or
thenAcceptAsync,
potentially
with
a
specified
executor.
The
method
requires
a
non-null
value;
passing
null
typically
results
in
a
NullPointerException.
ready-made
result
in
a
pipeline.
For
instance,
you
can
write:
CompletableFuture<String>
f
=
CompletableFuture.completedFuture("ok");
and
then
chain
further
processing
that
will
run
immediately
if
invoked
on
this
already-completed
future.
a
valid
result
is
supplied.
Related
concepts
include
completing
a
future
exceptionally
(completeExceptionally)
and,
in
newer
Java
versions,
failedFuture
for
representing
an
already-failed
computation.
Completed
futures
are
useful
for
testing,
stubbing,
or
providing
deterministic
values
within
a
broader
asynchronous
workflow.
pipelines
without
initiating
new
asynchronous
work.