Executor framework is for general-purpose concurrency; and usually independent tasks.
Fork-Join framework is for parallel computation, e.g., array sum, sorting, tree traversal; and tasks that can be split recursively into smaller tasks (so divide-and-conquer can be applied)
Split a task into smaller subtasks which can be executed concurrently.
Sum left = new Sum(array, low, mid);
left.fork();
Here Sum is a subclass of RecursiveTask and left.fork() spilts the task into sub-tasks.
Combine results of subtasks to produce the final result once they have finished executing, otherwise it keeps waiting.
left.join();
Here left is an object of Sum class.
Uses work-stealing: idle threads “steal” tasks from busy threads to balance load.
Typically, you don’t manually manage threads here—let the pool handle it.
ForkJoinPool pool = new ForkJoinPool(); // default parallelism = number of cores
Two main subclasses:
RecursiveTask<V> → Returns a result.
@Override
protected Long compute()
RecursiveAction → Does not return a result.
@Override
protected void compute()
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.ForkJoinPool;
// ------ SumTask -----
class SumTask extends RecursiveTask<Long> {
private final long[] array;
private final int start, end;
private static final int THRESHOLD = 10; // small enough to compute directly
public SumTask(long[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
if (end - start <= THRESHOLD) {
long sum = 0;
for (int i = start; i < end; i++) sum += array[i];
return sum;
} else {
int mid = (start + end) / 2;
SumTask left = new SumTask(array, start, mid);
SumTask right = new SumTask(array, mid, end);
left.fork(); // asynchronously compute left
long rightResult = right.compute(); // compute right in current thread
long leftResult = left.join(); // wait for left result
return leftResult + rightResult;
}
}
}
// ------ Main Class -----
public class ForkJoinExample {
public static void main(String[] args) {
long[] array = new long[100];
for (int i = 0; i < array.length; i++) array[i] = i+1;
ForkJoinPool pool = new ForkJoinPool();
SumTask task = new SumTask(array, 0, array.length);
long result = pool.invoke(task);
System.out.println("Sum = " + result); // 5050
}
}
Efficient parallelism: Works very well for recursive, divisible tasks.
Work-stealing: Keeps threads busy and reduces idle time.
Automatic load balancing: No need to manually split threads.
Overhead: Splitting too finely can hurt performance.
Not ideal for tasks with heavy I/O; better for CPU-bound tasks.
Use ForkJoinPool.commonPool() for quick tasks.