Java Fork/Join Framework & RecursiveTask
The Fork/Join Framework, introduced in Java 7, is a parallel execution engine built around the divide-and-conquer paradigm. It powers parallel streams, Arrays.parallelSort, and CompletableFuture's default pool. Understanding it lets you write fine-tuned parallel algorithms that exploit all available CPU cores through work stealing.
Contents
Core Concepts & Work Stealing
The Fork/Join Framework splits a large task into smaller subtasks recursively, executes them in parallel, and merges results. The key innovation is work stealing: idle threads steal tasks from the tail of busy threads' deques, keeping all cores saturated with minimal synchronization overhead.
The two base classes are:
: computes and returns a resultRecursiveTask<V> : computes without returning a value (side effects)RecursiveAction
RecursiveTask: Returning a Value
Classic example: parallel array sum. Below the threshold the task computes directly; above it, the range is halved, both halves forked, then joined.
import java.util.concurrent.RecursiveTask;
public class ParallelSum extends RecursiveTask<Long> {
private static final int THRESHOLD = 5_000;
private final long[] data;
private final int lo, hi;
public ParallelSum(long[] data, int lo, int hi) {
this.data = data;
this.lo = lo;
this.hi = hi;
}
@Override
protected Long compute() {
int size = hi - lo;
if (size <= THRESHOLD) {
// Base case โ sequential sum
long sum = 0;
for (int i = lo; i < hi; i++) sum += data[i];
return sum;
}
// Recursive case โ split in half
int mid = lo + size / 2;
ParallelSum left = new ParallelSum(data, lo, mid);
ParallelSum right = new ParallelSum(data, mid, hi);
left.fork(); // submit left to pool asynchronously
long rightResult = right.compute(); // compute right in current thread
long leftResult = left.join(); // wait for left
return leftResult + rightResult;
}
}
fork()compute()import java.util.concurrent.ForkJoinPool;
long[] data = new long[1_000_000];
// โฆ fill data โฆ
ForkJoinPool pool = ForkJoinPool.commonPool();
long total = pool.invoke(new ParallelSum(data, 0, data.length));
System.out.println("Sum: " + total);
RecursiveAction: Side-Effect Tasks
RecursiveAction
import java.util.concurrent.RecursiveAction;
public class ParallelNormalizer extends RecursiveAction {
private static final int THRESHOLD = 2_000;
private final double[] data;
private final int lo, hi;
private final double max;
public ParallelNormalizer(double[] data, int lo, int hi, double max) {
this.data = data; this.lo = lo; this.hi = hi; this.max = max;
}
@Override
protected void compute() {
if (hi - lo <= THRESHOLD) {
for (int i = lo; i < hi; i++) data[i] /= max;
return;
}
int mid = lo + (hi - lo) / 2;
invokeAll(
new ParallelNormalizer(data, lo, mid, max),
new ParallelNormalizer(data, mid, hi, max)
);
}
}
invokeAll()
ForkJoinPool: Configuration & Submission
You can use the shared common pool or create a dedicated pool with a specific parallelism level.
// Shared common pool โ parallelism = Runtime.getRuntime().availableProcessors() - 1
ForkJoinPool common = ForkJoinPool.commonPool();
System.out.println("Parallelism: " + common.getParallelism());
// Dedicated pool โ useful for isolating latency-sensitive workloads
ForkJoinPool custom = new ForkJoinPool(
4, // parallelism
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, // uncaught exception handler
false // asyncMode (LIFO vs FIFO)
);
try {
Long result = custom.invoke(new ParallelSum(data, 0, data.length));
System.out.println("Result: " + result);
} finally {
custom.shutdown();
}
// Three submission methods:
// pool.invoke(task) โ blocks until done, returns result
// pool.submit(task) โ returns Future<T>, non-blocking
// pool.execute(task) โ fire-and-forget (RecursiveAction)
ManagedBlockerFork/Join vs invokeAll Pattern
Two idiomatic patterns for splitting work:
// Pattern 1 โ fork/compute/join (most efficient for 2 subtasks)
left.fork();
long r2 = right.compute();
long r1 = left.join();
return r1 + r2;
// Pattern 2 โ invokeAll (cleaner for N subtasks)
List<ParallelSum> subtasks = new ArrayList<>();
for (int[] range : splitRanges(lo, hi, 4)) {
subtasks.add(new ParallelSum(data, range[0], range[1]));
}
invokeAll(subtasks); // forks all, then joins all
return subtasks.stream().mapToLong(RecursiveTask::join).sum();
Choosing the Right Threshold
The threshold controls the granularity of parallelism. Too small โ massive overhead from task creation. Too large โ poor core utilization.
// Rule of thumb: aim for leaf tasks that take ~100ยตsโ1ms each
// For CPU-bound work on an 8-core machine processing 1M elements:
// 1_000_000 / 8 = 125_000 elements/core at minimum
// A threshold of 10_000โ50_000 usually works well
// Adaptive threshold based on available parallelism:
int threshold = Math.max(1000,
data.length / (ForkJoinPool.commonPool().getParallelism() * 4));
Common Pool & Parallel Streams
Parallel streams and Arrays.parallelSort
// Parallel stream internally uses ForkJoinPool.commonPool()
long sum = LongStream.rangeClosed(1, 1_000_000)
.parallel()
.sum();
// Running a parallel stream inside a custom ForkJoinPool scopes it to that pool
ForkJoinPool custom = new ForkJoinPool(2); // limit to 2 threads
long result = custom.submit(() ->
LongStream.rangeClosed(1, 1_000_000).parallel().sum()
).get();
custom.shutdown();
ForkJoinPoolPitfalls & Best Practices
The class below shows the implementation. Key points are highlighted in the inline comments.
// โ WRONG โ forking both subtasks and joining both
left.fork();
right.fork();
return left.join() + right.join(); // right.join() runs on current thread doing nothing useful
// โ
CORRECT โ fork one, compute the other in current thread
left.fork();
long r = right.compute();
return left.join() + r;
// โ WRONG โ blocking inside a task (starves the pool)
@Override
protected Long compute() {
Thread.sleep(1000); // blocks a ForkJoin worker thread!
return 42L;
}
// โ
CORRECT โ use ManagedBlocker for blocking operations
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() {
volatile boolean done = false;
@Override public boolean block() throws InterruptedException {
Thread.sleep(1000);
done = true;
return true;
}
@Override public boolean isReleasable() { return done; }
});
// โ WRONG โ shared mutable state across subtasks (data race)
long[] shared = new long[1]; // shared across tasks
@Override protected Long compute() { shared[0] += localSum; return shared[0]; }
// โ
CORRECT โ each task returns its own value; combine in parent