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

Carvia Tech | April 23, 2019 | 2 min read | 37 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, how would you sort them in Big O(n) time
  2. What is ThreadLocal in Java, where will you use this class
  3. What are four principles of OOP, How aggregation is different than Composition?
  4. How will you implement a Blocking Queue in Java
  5. How will you calculate factorial of a large number in Java
  6. What is Deadlock in Java? How to troubleshoot and how to avoid deadlock
  7. How will you implement your custom threadsafe Semaphore in Java



Find more on this topic:
Core Java image
Core Java

Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info