Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.07 KB | None | 0 0
  1. import akka.actor._
  2. // import akka.actor.Actor
  3. // import akka.actor.ActorSystem
  4. // import akka.actor.Props
  5.  
  6. // 1. >
  7.  
  8.  
  9.  
  10. class Player(msg: String) extends Actor {
  11.   var max = 4
  12.   def receive = {
  13.     case (from: ActorRef) => {
  14.       if (max > 0) {
  15.         max -= 1
  16.         println(msg)
  17.         from ! self
  18.       } else {
  19.              context.stop(self) //zakończenie działania aktora, nie kończy działania systemu Akka
  20.             context.system.terminate //wyłączłem, żeby drugie zad. działało
  21.       }
  22.     }
  23.   }
  24. }
  25.  
  26.  
  27. // < 1.
  28.  
  29. object Main {
  30.   def main(args: Array[String]) {
  31.     // ActorSystem is a heavy object: create only one per application
  32.     val ourSystem = ActorSystem("MySystem")
  33.     // 1. >
  34.     // val ping =  ourSystem.actorOf(Props[classOf[Player], "ping"))
  35.     // val pong =  ourSystem.actorOf(Props[classOf[Player], "pong")) //poleceany na wykładzie, ale ma ograniczenia i nie działa
  36.     val ping = ourSystem.actorOf(Props(new Player("PING")))
  37.     val pong = ourSystem.actorOf(Props(new Player("pong")))
  38.     ping ! pong
  39.     // < 1.
  40.    
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement