如何设置用于par的线程数 [英] How to set the number of threads to use for par
本文介绍了如何设置用于par的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道,您可以设置用于所有.par操作的线程数,如下所示:
collection.parallel.ForkJoinTasks.defaultForkJoinPool.setParallelism(parlevel: Int)
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)
但是是否可以设置仅用于一个.par调用的线程数?
But is it possible to set the number of threads to use for just one .par call?
推荐答案
您可以创建一个设置并行度的块,然后在该块内执行特定的方法:
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
}
然后这样称呼它:
withParallelism(2) {
(1 to 100).par.map(_ * 2)
}
这篇关于如何设置用于par的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文