scala为了充分使用多核cpu,提供了并行集合(有别于前面的串行集合),用于多核环境的并行计算

    主要用到的算法有:
    Divide and conquer: 分治算法,scala通过splitters,combiners等抽象层来实现,主要原理是将计算工作分解很多任务,分发给一些处理器去完成,并将他们处理结果合并返回

    Work stealin: 算法,主要用于任务调度负载均衡(load-balancing),通俗点说,就是完成自己的所有任务之后,发现其他人还有活没干完,主动(或被安排)帮他人一起干,这样达到尽早干完的目的

    1. scala> val x = List(1, 2, 3, 4, 5, 6)
    2. x: List[Int] = List(1, 2, 3, 4, 5, 6)
    3. //转换为并行化集合
    4. scala> x.par
    5. res3: scala.collection.parallel.immutable.ParSeq[Int] = ParVector(1, 2, 3, 4, 5, 6)
    6. scala> x.par.sum
    7. res4: Int = 21

    注意fold

    1. val arr = Array(1, 2, 3, 4, 5, 6)
    2. println(arr.par.sum) //21
    3. println(arr.par.fold(10)(_+_)) //81

    因为fold的初始值在每个线程中都有
    如果想避免这个,就用有特定顺序的

    1. println(arr.par.foldLeft(10)(_+_)) //31

    提示: foldLeft和foldRight有一种缩写方法对应分别是:/:\

    1. val list4 = List(1, 9, 2, 8)
    2. # 0是初始值
    3. # 相当于 val i6 = (0 /: list4)( (res, next) => res - next)
    4. val i6 = (0 /: list4)(_ - _)
    5. println(i6)
    1. val sentence = "落花人独立,微雨燕双飞"
    2. # 字符串也是字符的集合
    3. # 取出一个字到剩余集合里面找
    4. val i7 = (Map[Char, Int]() /: sentence)((m, c) => m + (c -> (m.getOrElse(c, 0) + 1)))
    5. println(i7)

    记录中间值

    1. val i8= (1 to 10).scanLeft(0)(_ + _)
    2. println(i8)

    查看处理的线程

    1. val result1 = (0 to 1000).map{case _ => Thread.currentThread.getName}.distinct
    2. val result2 = (0 to 1000).par.map{case _ => Thread.currentThread.getName}.distinct
    3. println(result1)
    4. println(result2)

    输出

    1. Vector(main)
    2. ParVector(scala-execution-context-global-11, scala-execution-context-global-17, scala-execution-context-global-15, scala-execution-context-global-13, scala-execution-context-global-16, scala-execution-context-global-12, scala-execution-context-global-14, scala-execution-context-global-18)