Reputation: 12806
I know that you can set the number of threads to use for all .par operations like so:
collection.parallel.ForkJoinTasks.defaultForkJoinPool.setParallelism(parlevel: Int)
But is it possible to set the number of threads to use for just one .par call?
Upvotes: 19
Views: 9292
Reputation: 1606
In Scala 2.11 you should use parallel collection task support, like this:
parallelCollection.tasksupport = new ForkJoinTaskSupport(
new java.util.concurrent.ForkJoinPool(parlevel))
parallelCollection.map( ... )
See task support documentation
Upvotes: 20
Reputation: 2829
You could create a block that sets the parallelism level and then execute specific methods within that block:
def withParallelism[A](n : Int)(block : => A) : A = {
import collection.parallel.ForkJoinTasks.defaultForkJoinPool._
val defaultParLevel = getParallelism
setParallelism(n)
val ret = block
setParallelism(defaultParLevel)
ret
}
And then call it as such:
withParallelism(2) {
(1 to 100).par.map(_ * 2)
}
Upvotes: 17