How will you increment each element of an Integer array, using parallel operation
Carvia Tech | April 23, 2019 | 2 min read | 347 views
Java 7ForkJoinPool
, Java 8Arrays.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.
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:
- Given a collection of 1 million integers, all ranging between 1 to 9, sort them in Big O(n) time
- Java 8 Parallel Stream custom ThreadPool
- How will you implement your custom threadsafe Semaphore in Java
- What are four principles of OOP, How aggregation is different than Composition?
- Discuss internals of a ConcurrentHashmap (CHM) in Java
- Removing elements while iterating over a Java Collection
- What will happen if we don't synchronize getters/accessors of a shared mutable object in multi-threaded applications
Find more on this topic:
Subscribe to Interview Questions
Recommended books for interview preparation:
Book you may be interested in..
Book you may be interested in..
Similar Posts
- Code review checklist for Java developers
- Count word frequency in Java
- Secure OTP generation in Java
- HmacSHA256 Signature in Java
- Submit Form with Java 11 HttpClient - Kotlin
- Java Exception Class Hierarchy
- Http download using Java NIO FileChannel
- CRC32 checksum calculation Java NIO
- Precision and scale for a Double in java
- Difference between HashMap, LinkedHashMap and TreeMap