Reputation: 45732
Is using of parallel stream insdead of executor services in Java considered as bad practice? Why?
As you know myList.parallelStream().map(e -> ...)
will use ForkJoinPool.common()
under the hood. So if you will use at least two parallel streams at the same time you can face issues when:
map
function is blocking. But there is ForkJoinPool.ManagedBlocker as a rescue.map
function can be very CPU intensive which will cause other parallel streams to starve. Is there any way to set priority among RecursiveTask
s or between ForkJoinPool
s?In other hand you can create as many ForkJoinPool
s as you want. new ForkJoinPool(4).submit(() -> myList.parallelStream()...
. Is it considered performance-wise to use multiple ForkJoinPool
s at one JVM?
Use or not parallel stream = use or not ForkJoinPool, right? I found this and this links pretty useful to asnwer the last question
Upvotes: 4
Views: 3822
Reputation: 479
Here is very interesting talk which shares some wisdom around the question "when to consider using parallel streams?", shared by Brian Goetz himself. He takes over in the second part of this seminar: https://www.youtube.com/watch?v=2nup6Oizpcw&t=25m43s
Brian Goetz is the Java Language Architect at Oracle, and was the specification lead for JSR-335 (Lambda Expressions for the Java Programming Language.) He is the author of the best-selling Java Concurrency in Practice, as well as over 75 articles on Java development, and has been fascinated by programming since Jimmy Carter was President.
Upvotes: 2
Reputation: 3557
There isn't a one fits all solution for .parallel()
. Joschua Bloch says:
[...] do not even attempt to parallelize a stream pipeline unless you have good reason to believe that it will preserve the correctness of the computation and increase its speed. The cost of inappropriately parallelizing a stream can be a program failure or performance disaster. If you believe that parallelism may be justified, ensure that your code remains correct when run in parallel, and do careful performance measurements under realistic conditions. If your code remains correct and these experiments bear out your suspicion of increased performance, then and only then parallelize the stream in production code.
-Effective Java 3rd Edition, page 225, Item 28: Use caution when making streams parallel
He recommends you do a thorough benchmark under realistic conditions and decide on a case by case basis. Also, not only can using .parallel()
lead to bad performance, it can also lead to safety failures:
Safety failures may result from parallelizing a pipeline that uses mappers, filters, and other programmer-supplied function objects that fail to adhere to their specifications.
-Effective Java 3rd Edition, page 224, Item 28: Use caution when making streams parallel
To answer your question, it isn't considered bad practice, but you should use utmost caution when working with .parallel()
and not blindly slap it on every stream in your code base.
Upvotes: 5