summaryrefslogtreecommitdiff
path: root/test/pending/shootout/message.scala
blob: a7a1dacc9deb5c9dbab3b7b437983400bbea4a80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* The Computer Language Shootout
   http://shootout.alioth.debian.org/
   contributed by Isaac Gouy
*/


import scala.concurrent._ 

object message {
   def main(args: Array[String]) = {
      val n = Integer.parseInt(args(0))
      val nActors = 500
      val finalSum = n * nActors

      case class Message(value: Int)

      class Incrementor(next: Pid) extends Actor {
         var sum = 0

         override def run() = {
            while (true) {
               receive { 
                  case Message(value) => 
                     val j = value + 1 
                     if (null != next){ 
                        next ! Message(j) 
                     } else { 
                        sum = sum + j
                        if (sum >= finalSum){ 
                           Console.println(sum); 
                           System.exit(0) // exit without cleaning up
                        }
                     } 
               }
            }
         }

         def pid() = { this.start; this.self }
      }

      def actorChain(i: Int, a: Pid): Pid = 
         if (i > 0) actorChain(i-1, new Incrementor(a).pid ) else a

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