Created
August 8, 2012 06:27
-
-
Save infynyxx/3292824 to your computer and use it in GitHub Desktop.
Scala Evented Actors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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