Home

ForkJoinTask

ForkJoinTask is an abstract class in the Java concurrency framework that represents a unit of work in the Fork/Join framework. It is designed for tasks that can be recursively decomposed into subtasks, executed in parallel, and combined to produce a result. The class serves as the common base for tasks executed by a ForkJoinPool, the specialized executor that supports work-stealing to maximize processor utilization.

Two concrete specializations are RecursiveTask<V>, which computes and returns a value of type V, and RecursiveAction,

ForkJoinTask implements Future<V> and RunnableFuture<V>, allowing traditional Future operations such as get() and cancel(). It also

Usage patterns emphasize small, independent subtasks with minimal shared state. Because of the overhead, it's best

which
performs
computations
without
returning
a
value
(i.e.,
V
is
Void).
Subclasses
override
the
compute()
method
to
implement
the
task's
logic.
Within
compute(),
a
task
may
split
itself
into
subtasks
by
creating
new
ForkJoinTask
instances
and
invoking
fork()
on
them,
or
by
directly
invoking
compute()
on
subtasks.
After
forking,
a
task
typically
calls
join()
to
wait
for
a
subtask's
completion
and
obtain
its
result.
provides
methods
such
as
isDone(),
isCancelled(),
and
invokeAll()
to
coordinate
multiple
tasks.
Tasks
are
submitted
to
a
ForkJoinPool,
which
manages
worker
threads
and
uses
work-stealing
to
balance
load.
for
compute-intensive
tasks
with
fine-grained
decomposition
rather
than
large
blocking
operations.
Effective
use
involves
designing
tasks
that
can
be
distributed
into
parallelizable
units
and
then
recombined
efficiently,
leveraging
the
pool’s
ability
to
steal
work
from
busy
threads
to
maintain
throughput.