0

How can I compute sum of very large array concurrently by dividing into multiple CompletableFuture and trying to compute their result async.

For my simple sample program, I am using array length=4 and dividing into 2 CompletableFuture , but how can I make the process dynamic if the array size is in millions. Ideally would want to keep the CPU occupied and they should keep giving their result as soon as the complete so that the global sum is returned as soon as possible.

public class Parallelism {

    public static void main(String[] args) {

        int[] arr = new int[]{1,2,3,4};

        //sum = 10
        int n = arr.length;

        CompletableFuture<Integer> first = CompletableFuture.supplyAsync(() -> {
           return sum(arr, 0, 1);
        });


        CompletableFuture<Integer> second = CompletableFuture.supplyAsync(() -> {
            return sum(arr, 2, n-1);
        });

        int sum = 0;

        sum += first.join();

        sum += second.join();

        assert sum == 10;
    }

    private static int sum(int[] arr, int start, int end) {
        int sum = 0;
        for(int i = start; i <= end; i++) {
            sum += arr[i];
        }
        return sum;
    }
}
Novice User
  • 3,552
  • 6
  • 31
  • 56
  • Is this not dependant upon the number of CPU/cores that you have? – Scary Wombat Oct 31 '18 at 05:17
  • `Runtime.getRuntime().availableProcessors();` might be a good starting place, but I believe the OS might keep scheduling other priority threads and put my threads to the queue. So essentially I would want to divide the task into some efficient number of threads, granular enough so that they return their local sum early. And the larger question is how can I use `CompletableFuture` dynamically for this purpose. – Novice User Oct 31 '18 at 05:19
  • Not an answer, but couldn't you just put `IntStream.of(arr).parallel().sum()` inside a `CompletableFuture.supplyAsync` call? – Slaw Oct 31 '18 at 05:36
  • It will use `ForkJoinPool.commonPool` for the threads. I am not sure if that will be a better choice, since I want better control on how I create multiple threads based on the array length, and probably use a custom `ExecutorService` – Novice User Oct 31 '18 at 05:42
  • 1
    @NoviceUser Just a side note: you can use parallel `Stream`s with a custom `ForkJoinPool` - see [this answer](https://stackoverflow.com/a/22269778/2032415). – Tomasz Linkowski Oct 31 '18 at 10:29

0 Answers0