User Tools

Site Tools


Replicated Workers using ForkJoinPool in Java Threads

Utilizing Java's ForkJoinPool is a great approach for parallelizing tasks that can be broken down into smaller, recursive pieces.

It's especially effective for computational tasks that benefit from a divide-and-conquer strategy. In the context of the previous example of summing integers, ForkJoinPool can be used to divide the task of computing the sum into smaller subtasks, each summing a part of the range, and then combining the results.
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
public class SumCalculatorWithForkJoin {
    static class SumTask extends RecursiveTask<Integer> {
        private final int start;
        private final int end;
        private static final int THRESHOLD = 10;
        SumTask(int start, int end) {
            this.start = start;
            this.end = end;
        protected Integer compute() {
            if ((end - start) <= THRESHOLD) {
                return calculateSum(start, end);
            } else {
                int mid = start + (end - start) / 2;
                SumTask left = new SumTask(start, mid);
                SumTask right = new SumTask(mid + 1, end);
                left.fork(); // Asynchronously execute the left subtask
                return right.compute() + left.join(); // Compute right subtask and wait for the left subtask
        private int calculateSum(int start, int end) {
            int sum = 0;
            for (int i = start; i <= end; i++) {
                sum += i;
            return sum;
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool();
        int n = 100; // Sum of first 100 integers
        int totalSum = pool.invoke(new SumTask(1, n));
        System.out.println("Total sum of first " + n + " integers: " + totalSum);

SumTask Class: This class extends RecursiveTask<Integer> and represents a task that calculates the sum of integers in a specified range (start to end). If the range is smaller than a predefined threshold (THRESHOLD), it calculates the sum directly. If larger, it splits the task into two subtasks.

Task Splitting: The sum task is split into two smaller tasks (left and right) if the range exceeds the THRESHOLD. This divide-and-conquer approach allows the problem to be solved in parallel.

ForkJoinPool: The ForkJoinPool manages the execution of these tasks. The fork() method is used to asynchronously execute subtasks, and join() waits for the completion of these subtasks.

Main Method: In the main method, a ForkJoinPool is created, and a SumTask is submitted to it with the range from 1 to 100. The pool's invoke method starts the task and waits for the result.

This implementation demonstrates how ForkJoinPool can be effectively used for parallel computation in Java, especially to a sequential approach. This makes it an excellent choice for computationally intensive tasks or for processing large datasets where task subdivision can lead to significant performance improvements.

The ForkJoinPool framework is an essential tool in modern Java for achieving efficient parallel processing.

java/threads/replicated-workers-fork-join-pool.txt · Last modified: 2024/01/17 04:20 by odefta