Java serial vs parallel stream

use parallel stream when:
1) operation applied to data element is independent
2) either
a) cpu intensive (e.g. isPrime(number))
b) there are many of these data elements and they are efficiently splitable (like collections with random access and/or efficient search e.g. ArrayList; also HashMap and plain arrays, as opposed to not so efficiently splittable LinkedList, BlockinQueue and most IO based)

– does NOT make sense to paralellize relatively small number (e.g. less than 10000) of cpu non-intensive (not time-consuming) oprerations, instead use serial stream:
*it may take some startup time for JVM to start all the cores for parallel processing,
* tasks subdivision takes also time for the framework to makes little sense to divide further already very small tasks
– it does make sense to paralellize cpu intensive operations: numbers.parallelStream().filter(this::isPrime).forEach(System.out::print)

Note that most IO sources (e.g. read lines) are not efficiently splitable and should be used sequentially.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s