How will you increment each element of an Integer array, using parallel operation

Upasana | April 23, 2019 | 2 min read | 347 views


Java 7 ForkJoinPool, Java 8 Arrays.parallelSetAll can be used for performing parallel operations on an integer array.

Using ForkJoinPool

We can use ForkJoin framework to divide this task recursively into multiple sub tasks and fork them using multiple cpu’s available to the JVM. Java 7 provides us with RecursiveAction class that can be extended to utilize ForkJoinPool framework with a great ease.

divideandconquer
Dividing task recursively
IncrementTask.java
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

class IncrementTask extends RecursiveAction {
    private final int THRESHOLD = 100;
    final long[] array;
    final int lo, hi;

    IncrementTask(long[] array, int lo, int hi) {
        this.array = array;
        this.lo = lo;
        this.hi = hi;
    }

    protected void compute() {
        if (hi - lo < THRESHOLD) {
            for (int i = lo; i < hi; ++i)
                array[i]++;
        } else {
            int mid = (lo + hi) >>> 1;
            invokeAll(new IncrementTask(array, lo, mid), new IncrementTask(array, mid, hi));
        }
    }

    public static void main(String[] args) {
        long[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        IncrementTask incrementTask = new IncrementTask(array, 0, 9);
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        forkJoinPool.invoke(incrementTask);
        System.out.println("array = " + Arrays.toString(array));
    }
}
Program output
array = [2, 3, 4, 5, 6, 7, 8, 9, 10]

Using Array parallel operation in Java 8

Java 8 provides great support for parallel operations on array using standard library functions.

Java 8 style array parelle operation
long[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println("array = " + Arrays.toString(array));
Arrays.parallelSetAll(array, x -> array[x] + 1);    (1)
System.out.println("array = " + Arrays.toString(array));
1 x is the current index of array
Program output
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array = [2, 3, 4, 5, 6, 7, 8, 9, 10]

As we can see Java 8 style parallel operations are much simpler than plain ForkJoinPool implementation.


Top articles in this category:
  1. Given a collection of 1 million integers, all ranging between 1 to 9, sort them in Big O(n) time
  2. Java 8 Parallel Stream custom ThreadPool
  3. How will you implement your custom threadsafe Semaphore in Java
  4. What are four principles of OOP, How aggregation is different than Composition?
  5. Discuss internals of a ConcurrentHashmap (CHM) in Java
  6. Removing elements while iterating over a Java Collection
  7. What will happen if we don't synchronize getters/accessors of a shared mutable object in multi-threaded applications

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.