返回顶部

收藏

Scala Actors

更多

message.scala

package examples.actors

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

object Message {
  def main(args: Array[String]) {
    val n = try {
      Integer.parseInt(args(0))
    }
    catch {
      case _ =>
        println("Usage: examples.actors.Message <n>")
        Predef.exit
    }
    val nActors = 500
    val finalSum = n * nActors
    Scheduler.impl = new SingleThreadedScheduler

    def beh(next: Actor, sum: Int) {
      react {
        case value: Int =>
          val j = value + 1; val nsum = sum + j
          if (next == null && nsum >= finalSum) {
            println(nsum)
            System.exit(0)
          }
          else {
            if (next != null) next ! j
            beh(next, nsum)
          }
      }
    }

    def actorChain(i: Int, a: Actor): Actor =
      if (i > 0) actorChain(i-1, actor(beh(a, 0))) else a

    val firstActor = actorChain(nActors, null)
    var i = n; while (i > 0) { firstActor ! 0; i -= 1 }
  }
}

标签:Actors,Scala

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2015-06-16 09:31:44pingpong.scala by 王忠强
  2. 2015-04-11 15:49:24解析一段日志 by owensh
  3. 2015-06-13 14:48:07将一个Char字符转换成unicode字符串表示 by bargain
  4. 2015-08-04 13:35:16Scala 时间处理一例 by 睡到自然醒
  5. 2012-11-11 19:49:44用scala实现的nio事件轮询 by 卷毛
  6. 2015-04-21 09:17:54Scala 调用 cc 解释器 by xfk
  7. 2015-06-15 09:06:23HTTP请求封装 by 凤歌
  8. 2015-08-04 13:40:48Scale 之长整型 bigint by 风云轩
  9. 2012-12-11 09:32:51Scala 定义复杂操作符 by moxia
  10. 2015-04-23 09:05:02快速匹配字符串 by Kevin.
  11. 2019-06-17 21:18:10PHP+jQuery.photoClip.js图片裁剪上传实例 by 素材火官网

发表评论