Improved ping pong example.

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@10896 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
phaller 2007-05-01 09:34:19 +00:00
parent 97c622998f
commit c28622e86c
1 changed files with 9 additions and 12 deletions

View File

@ -3,28 +3,26 @@ package examples.actors
import scala.actors.Actor
import scala.actors.Actor._
case object SendPing
case object Ping
case object Pong
case object Stop
class Ping(count: int, pong: Actor) extends Actor {
def act() {
var pingsLeft = count
var pingsLeft = count - 1
pong ! Ping
loop {
react {
case SendPing =>
pong ! Ping
pingsLeft = pingsLeft - 1
case Pong =>
if (pingsLeft % 1000 == 0)
Console.println("Ping: pong")
if (pingsLeft > 0)
self ! SendPing
else {
if (pingsLeft > 0) {
pong ! Ping
pingsLeft -= 1
} else {
Console.println("Ping: stop")
pong ! Stop
exit('stop)
exit()
}
}
}
@ -40,10 +38,10 @@ class Pong extends Actor {
if (pongCount % 1000 == 0)
Console.println("Pong: ping "+pongCount)
sender ! Pong
pongCount = pongCount + 1
pongCount += 1
case Stop =>
Console.println("Pong: stop")
exit('stop)
exit()
}
}
}
@ -54,5 +52,4 @@ object pingpong extends Application {
val ping = new Ping(100000, pong)
ping.start
pong.start
ping ! SendPing
}