Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.85 KB | None | 0 0
  1. case object Ping
  2. case object Pong
  3. case object Stop
  4.  
  5. import scala.actors.Actor
  6. import scala.actors.Actor._
  7.  
  8.  
  9. class Ping(x: Int, pong: Actor) extends Actor {
  10.   def act() {
  11.     var pingsToGo = x
  12.     pong ! Ping
  13.     println("Ping numer: " + pingsToGo)
  14.     while(true) {
  15.       receive {
  16.         case Pong =>
  17.           if(pingsToGo != 0) {
  18.             pingsToGo-=1
  19.             println("Ping numer: " + pingsToGo)
  20.             pong ! Ping
  21.           } else {
  22.             pong ! Stop
  23.             exit()
  24.           }
  25.       }
  26.     }
  27.   }
  28. }
  29.  
  30. class Pong extends Actor {
  31.   def act() {
  32.     while(true) {
  33.       receive {
  34.         case Ping =>
  35.           print("Pong")
  36.           sender ! Pong
  37.         case Stop =>
  38.           println("Koniec.")
  39.           exit()
  40.       }
  41.     }
  42.   }
  43. }
  44.  
  45. val pong = new Pong
  46. val ping = new Ping(10, pong)
  47. pong.start
  48. ping.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement