Home

supplyAsync

SupplyAsync is a static factory method of the CompletableFuture class in java.util.concurrent. It creates a new CompletableFuture that will be completed when the provided Supplier finishes executing in another thread. The computation is started asynchronously by submitting the supplier to an Executor.

The variant supplyAsync(Supplier<U> supplier) uses the ForkJoinPool.commonPool() as the default executor, while supplyAsync(Supplier<U> supplier, Executor executor)

Use cases include starting asynchronous computations without managing threads directly, enabling non-blocking programming patterns. The method

Cancellation and error handling: The returned future can be canceled. If canceled before execution, the task

Origin and scope: Introduced in Java 8 as part of the CompletableFuture API, supplyAsync provides a concise

uses
the
supplied
executor.
The
supplier
is
invoked
once
in
that
background
thread,
and
the
returned
CompletableFuture<U>
is
completed
with
the
supplier's
result.
If
the
supplier
throws
an
exception,
the
CompletableFuture
completes
exceptionally
with
that
exception.
returns
immediately,
allowing
subsequent
actions
such
as
thenApply,
thenAccept,
thenCompose,
whenComplete,
or
handle
to
be
chained
once
the
result
is
available.
may
be
prevented
from
running;
if
already
running,
cancellation
depends
on
the
executor
and
task
responsiveness.
Exceptions
thrown
by
the
supplier
are
captured
and
propagated
to
downstream
stages
as
completion
exceptions.
When
results
are
retrieved
via
get,
ExecutionException
typically
wraps
the
original
cause.
means
to
initiate
asynchronous
work
without
explicit
thread
creation,
fitting
into
asynchronous
programming
patterns
common
in
modern
Java
applications.