拥有.NET背景我试图为这里的并行集合实现一些“扩展方法”:
implicit class ParSeqExtensions[T](pc : ParSeq[T]) {
/** limits the number of threads of parallel collection - the equivalent of C# DegreeOfParallelizm*/
def withDegreeOfParallelism(numberOfThreads: Int): ParSeq[T] = {
pc.tasksupport = new ForkJoinTaskSupport(new scala.concurrent.forkjoin.ForkJoinPool(numberOfThreads))
pc
}
}
implicit class ParMapExtensions[K,V](map: ParMap[K,V]) {
def withDegreeOfParallelism(numberOfThreads: Int): ParMap[K,V] = {
map.tasksupport = new ForkJoinTaskSupport(new scala.concurrent.forkjoin.ForkJoinPool(numberOfThreads))
map
}
}
taskSupport
在特质中定义:
ParIterableLike[+Long, +Beginner <: Unreadable[TypeParameters]]
对于每个ParXyz
集合类型分别实现相同的操作很无聊。斯卡拉是如此强大,一次成功吗?怎么样?