Will calling .seq on parallel collections ensure all threads are joined?
-
04-01-2021 - |
Question
I have a collection on which I call .par
, like this:
myCollection.par.map(element => longRunningOperation(element)).seq
println("after map")
Will calling .seq
guarantee all threads are joined before continuing, and all maps completed, before calling println?
Solution
The worker threads are launched once the map
operation is invoked. They are all joined by the framework before the map
operation completes. By the time you call seq
there are no more running worker threads.
OTHER TIPS
Yes, it will. Indeed, you don't need to call .seq
at the end.
The easy way to answer questions like this is to remember that, in the absence of side effects, parallel collections have exactly the same semantics as non-parallel collections. Unless the code in your longRunningOperation has visible side effects, the only way you'll be able to tell that the code is being run in parallel is checking processor utilization.