Skip to content

Instantly share code, notes, and snippets.

@jboner
Last active March 27, 2019 16:43
Show Gist options
  • Save jboner/9990472 to your computer and use it in GitHub Desktop.
Save jboner/9990472 to your computer and use it in GitHub Desktop.

Revisions

  1. jboner revised this gist Apr 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GameOfPingPong.scala
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ object PingPong extends App {
    case Ball =>
    counter += 1
    println(s"Ping counter: ${counter}")
    Thread.sleep(1000)
    Thread.sleep(1000) // you should *never* call Thread.sleep in a real application, just here to slow things down
    sender ! Ball
    }
    }
  2. jboner created this gist Apr 5, 2014.
    38 changes: 38 additions & 0 deletions GameOfPingPong.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    package demo

    import akka.actor.{Actor, Props, ActorSystem}

    object PingPong extends App {
    case object Ball

    class Ping extends Actor {
    var counter = 0

    def receive = {
    case Ball =>
    counter += 1
    println(s"Ping counter: ${counter}")
    Thread.sleep(1000)
    sender ! Ball
    }
    }

    class Pong extends Actor {
    var counter = 0

    def receive = {
    case Ball =>
    counter += 1
    println(s"Pong counter: ${counter}")
    Thread.sleep(1000)
    sender ! Ball
    }
    }

    val system = ActorSystem("pingpong")

    val ping = system.actorOf(Props(classOf[Ping]), "ping")
    val pong = system.actorOf(Props(classOf[Pong]), "pong")

    ping.tell(Ball, pong)
    }