Skip to content

Instantly share code, notes, and snippets.

@jamie-allen
Created July 6, 2011 20:15
Show Gist options
  • Save jamie-allen/1068213 to your computer and use it in GitHub Desktop.
Save jamie-allen/1068213 to your computer and use it in GitHub Desktop.
Duncan Example
import scala.actor._
import scala.actor.Actor._
sealed trait DoSomething
case class SaySomething(text: String) extends DoSomething
case class YellSomething(text: String) extends DoSomething
object DuncanExample {
def main(args: Array[String]) {
val nActors = 5
Scheduler.impl = new SingleThreadedScheduler
val mySupervisor = new MySupervisor
mySupervisor start
}
class MySupervisor extends Actor {
val myChildren = List(new MyChild, new MyChild, new MyChild)
myChildren.map(child => {
this link child
child start
})
def act(): Unit = {
trapExit = true // if you don't trap, this gets killed along with the one that died
def run(head: Actor): Nothing = react {
case Exit(deadActor, reason) => // do something, maybe create/start new instance, or just report it
case x => {
myChildren.map(child => child ! x)
run(head)
}
}
}
}
class MyChild extends Actor {
override def act : Unit = {
this.react {
case SaySomething(_) => println(_)
case YellSomething(_) => println(_.capitalize)
case _ => throw new Exception("nuts")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment