scala 并行地图操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20092855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parallel map operations?
提问by csvan
Does Scala provide a way to execute parallel map operations as part of the standard language?
作为标准语言的一部分,Scala 是否提供了一种执行并行映射操作的方法?
For example, given:
例如,给定:
scala> val a = List((1,2), (3,4), (3,6))
a: List[(Int, Int)] = List((1,2), (3,4), (3,6))
I can do:
我可以:
scala> a.map(tup => tup._1 + tup._2)
res0: List[Int] = List(3, 7, 9)
However, to the best of my knowledge this maps the provided function over the list objects sequentially. Is there a built-in way to have the function applied to each element in a separate thread (or equivalent), and the results then gathered into a resulting list?
但是,据我所知,这将提供的函数按顺序映射到列表对象上。是否有内置方法可以将函数应用于单独线程(或等效线程)中的每个元素,然后将结果收集到结果列表中?
回答by Akos Krivachy
If you add parthen you will get a parallel collection and operations on it will be processed in parallel. To convert back to a normal collection call a toList.
如果您添加,par那么您将获得一个并行集合,并且对它的操作将并行处理。要转换回普通集合,请调用toList.
So your code would look like:
所以你的代码看起来像:
a.par.map(tup => tup._1 + tup._2).toList
Or a .seqto get a Sequential Collection (the oppositeof a Parallel Collection).
或者 a.seq获得顺序集合(与并行集合相反)。
a.par.map(tup => tup._1 + tup._2).seq
回答by miko?ak
parsplits your list for processing over several threads. You can then regulate how the threading is done by modyfying the tasksupportmember of the resultant ParSeq.
par拆分您的列表以在多个线程上进行处理。然后,您可以通过修改 result 的tasksupport成员来调节线程是如何完成的ParSeq。

