Skip to content

Instantly share code, notes, and snippets.

@infynyxx
Created August 8, 2012 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infynyxx/3292824 to your computer and use it in GitHub Desktop.
Save infynyxx/3292824 to your computer and use it in GitHub Desktop.
Scala Evented Actors
import scala.actors.Actor
import Actor._
def buildChain(size: Int, next: Actor): Actor = {
val a = actor {
react {
case 'Die =>
val from = sender
if (next != null) {
next ! 'Die
react {
case 'Ack => from ! 'Ack
}
} else {
from ! 'Ack
}
}
}
if (size > 0) buildChain(size - 1, a)
else a
}
object EventActor {
def main(args: Array[String]) {
val numActors = args(0).toInt
val start = System.currentTimeMillis
println("Hello: " + numActors)
buildChain(numActors, null) ! 'Die
receive {
case 'Ack =>
val end = System.currentTimeMillis
println("took " + (end - start) + " ms")
case 'Die =>
println("End of Actor")
}
}
}
EventActor.main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment