我知道可以在actor系统外使用system.shutdown()
来停止actor系统,并使system.awaitTermination()
停止阻塞执行其线程。
我想从我的一个Actor内触发actor系统的关机。 我认为应该能够在Actor的接收方法中调用context.system.shutdown()
,但是当我这样做时,似乎什么都没有发生,system.awaitTermination()
(在主线程上)仍然继续阻塞。
有任何想法吗?
class WatchActor extends Actor {
val child = context.actorOf(Props.empty, "child")
context.watch(child) // <-- this is the only call needed for registration
var lastSender = system.deadLetters
def receive = {
case "kill" =>
context.stop(child); lastSender = sender()
case Terminated(`child`) => lastSender ! "finished"
}
}
http://doc.akka.io/docs/akka/snapshot/scala/actors.html
jfm
主线程:
val l = new CountDownLatch(1)
val s = ActorSystem("system", config)
...
//create some actors and send some messages to them
...
l.await()
s.shutdown()
s.awaitTermination()
在def receive: Actor.Receive
的某个地方:
l.countDown()
system.awaitTermination()
会一直阻塞,因为你正在等待它结束!context.system.shutdown()
,但你不能在Actor系统上下文中调用system.awaitTermination()
。而且它也没有太多意义:既然系统已经关闭了,为什么还要等待呢?system.awaitTermination()
。该调用在主线程上,也就是启动actor系统的线程上。我的问题是,当我从actor内部调用context.system.shutdown()
时,系统无法关闭,而主线程则一直阻塞等待关闭。 - sungiant