alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (message.scala)

This example Scala source code file (message.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

actor, array, array, incrementor, int, int, message, message, pid, pid

The Scala message.scala source code

/* 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 }
   }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala message.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.