Sunday, March 10, 2019

Java 8 Lambda Parallel Streams :: Use thread safe collections

Changing code to use parallelStream from stream is not as simple as it looks - there are several design principles that need to be accounted for.

The parallel stream code in the lambda libraries wrap the ForkJoin framework from Java 7.  There are several issues that will affect performance and accuracy of your code.

Using non-threadsafe collections - this will show up as randomly varying results and modifications of Lists and Maps if they use out of the box non-threadsafe implementations like ArrayList and HashMap.  Stick to CopyOnWriteArrayList and ConcurrentHashMap.

For example

Using a small dataset for parallel operations may be slower than sequential code because of the parallel framework overhead.  Try to use data sets that are at lease 10k in size

For example - filtering on 10 Million biometric records.

No comments:

Total Pageviews