FAQ
关于actor的一点疑惑,代码如下:

==========================================
import scala.actors.Actor._

/**
  * 区间内整数求和
  */
def countArea(floor:Int, top:Int) = {
     (0 /: (floor to top)) {
         (sum, i) => { sum + i }
     }
}

/**
  * 1到num所有整数求和
  */
def sumAll(num:Int) = {
     val RANGE = 10
     val partCount = (num.toDouble / RANGE).ceil.toInt
     val caller = self

     for (i <- 0 until partCount) {
         val floor = i * RANGE + 1;
         val top = num min (i + 1) * RANGE
         actor { caller ! countArea(floor, top) }
     }

     (0 /: (0 until partCount)) {
         (allSum, i) => receive {
             case sumInRange:Int => { allSum + sumInRange }
         }
     }
}

println(sumAll(100))
==========================================

sumAll方法按每段10个数字把整个区间分成多段同时调用countArea方法执行累加。执行的结果显示也确实是并行执行的。

我的疑惑是:按照Actor.self()方法的定义应该是得到关联当前线程actor。那既然是同一个线程(当前线程),并行性是如何实现的呢?

--
您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 1 | next ›
Discussion Overview
groupscalacn @
postedSep 3, '13 at 10:09a
activeSep 3, '13 at 10:09a
posts1
users1

1 user in discussion

Jade Shan: 1 post

People

Translate

site design / logo © 2019 Grokbase