QtConcurrent的Scala类似物 [英] Scala analogues of QtConcurrent

查看:73
本文介绍了QtConcurrent的Scala类似物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala(或Java)的QtConcurrent有哪些类似物?即简化了MapReduce,并行映射和foldl的实现. 谢谢

What are the analogues of QtConcurrent for Scala (or Java)? Ie simplified implementation of MapReduce, the parallel map and foldl. Thank you

推荐答案

您可以使用Scala并行集合.它们目前是Scala夜间发行版的一部分,并将在Scala 2.9中发行.这个想法是,常规集合中可用的大多数操作都是并行的,因此可以以相同的方式使用并行集合.

You can use Scala Parallel Collections. They are currently a part of Scala nightly releases, and will be released in Scala 2.9. The idea is that most operations available in regular collections are parallelized, so that parallel collections can be used in the same way.

当前,有一些可用的收集类型-并行范围,并行数组和并行哈希尝试.例如,您可以像这样在并行数组上调用并行的mapfold操作:

Currently, there are a few collection types available - parallel ranges, parallel arrays and parallel hash tries. For instance, you can invoke a parallel map and fold operations on a parallel array like this:

scala> val pa = (0 until 10000).toArray.par
pa: scala.collection.parallel.mutable.ParArray[Int] = ParArray(0, 1, 2, 3, 4, 5, 6,...

scala> pa.map(_ + 1)
res0: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4, 5, 6, 7,...

scala> pa map { v => if (v % 2 == 0) v else -v }
res1: scala.collection.parallel.mutable.ParArray[Int] = ParArray(0, -1, 2, -3, 4, -5,...

scala> pa.fold(0) { _ + _ }
res2: Int = 49995000

还有其他并行收集操作可用.请注意,fold必须采用关联运算符-在上面的示例中,加法是关联的((A + B)+ C == A +(B + C)),即,您可以按任意顺序添加数字的子序列,并且您可以总是会获得相同的金额(reduce具有相似的合同).

There are other parallel collection operations available as well. Note that fold must take an associative operator - in the example above, addition is associative ((A + B) + C == A + (B + C)), i.e. you can add subsequences of numbers in any order and you will always obtain the same sum (reduce has a similar contract).

要知道的另一件事是,传递给并行集合的闭包是同时调用的.如果它们具有副作用,例如在环境中修改局部变量,则必须同步这些访问.例如,您可以执行以下操作:

One other thing to be aware of is that the closures passed to parallel collections are invoked simultaneously. If they have side-effects, such as modifying a local variable in the environment, these accesses have to be synchronized. For instance, you could do something like this:

scala> var a = 0                                                                                                                                                                 
a: Int = 0                                                                                                                                                                       

scala> pa foreach { a += _ }                                                                                                                                                     

scala> a                                                                                                                                                                         
res1: Int = 49995000             

scala> a = 0
a: Int = 0

scala> pa foreach { a += _ }

scala> a
res7: Int = 49990086

每次都会有不同的结果,因为foreach并行调用{ a += _ }.在上面的示例中,应该使a同步,并使用锁或原子进行保护.

and have different results every time, because the foreach invokes { a += _ } in parallel. In the example above, a should be made synchronized, protected with a lock or atomic.

但是想法是使用内置的组合器来完成任务并倾向于功能编程,避免如上例所示的局部副作用.

But the idea is to use built-in combinators to accomplish a task and lean towards functional programming, avoiding local side-effects as in the example above.

您可能想在其他答案中提供的链接中详细了解它们的内部机制.

You might want to read a little bit more about their internal mechanisms in the links provided in the other answer.

这篇关于QtConcurrent的Scala类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆