Parallel stream processing

Java takes care of the parallel stream with its new stream API and the simplification of creating parallel processing on collections and arrays. Let's have a look at how this works.

Let's say myNumList is a list of integers containing 800.000 integer values. Before Java 8, these integer values were summed up using the for each loop; refer to the following code snippet:

for (int i :myList) {
result+= i;
}

Starting from Java 8, we can do the same thing using streams:

myList.stream().sum();

The next step is to parallelize this processing. We just need to use the parallelStream() instead of stream() or parallel() if we still have a stream, as shown in the following code snippet:

myList.parallelStream().sum();

The system needs to be reviewed where parallel processes can be performed and the performance of the application can be optimized by introducing parallel processing. Make sure your overall processing task is spawned as parallel processes do not wait for other resources; otherwise, the idea of parallel processing will provide no big advantage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset