Scala:链接的Actor没有终止

时间:2014-03-12 15:58:00

标签: scala actor

在scala.actors(不是Akka!)中,我希望一个发送专用的actor在另一个actor终止时立即终止。我相信scala.actors.Actor.link应该是合适的,但它不起作用。

这是我的代码:

import scala.actors.Actor
import scala.actors.Actor._

object Main extends App {
  class Receiver extends Actor {
    def act() {
      val sender = new Sender(self)
      sender.start()
      var i = 0
      while (i < 3) {
        receive {
          case 'ping => println('pong); i += 1
        }
      }
      exit('stop)
    }
  }
  class Sender(receiver: Actor) extends Actor {
    def act() {
      link(receiver) // <-- !!!
      while (true) {
        Thread sleep 500
        println("ping")
        receiver ! 'ping
      }
    }
  }
  val receiver = new Receiver
  receiver.start()
}

结果如下:

ping
'pong
ping
'pong
ping
'pong
ping
ping
ping
ping
ping
ping
ping
ping

发件人不会停止发送。为什么呢?

0 个答案:

没有答案