我知道在Future或调度程序中关闭sender
方法调用或actor的内部状态是不安全的,但是actor的上下文呢? ActorContext
中的内容是什么?在调度程序或将来的回调中关闭actor上下文是否安全? :
def receive: Receive = {
case Msg =>
system.scheduler.scheduleOnce(1 second) {
context.actorOf[ChildActor]
}
}
答案 0 :(得分:8)
不,关闭一个actor上下文是不安全的。来自akka来源:
/**
* Stores the context for this actor, including self, and sender.
* It is implicit to support operations such as `forward`.
*
* WARNING: Only valid within the Actor itself, so do not close over it and
* publish it to other threads!
*
* [[akka.actor.ActorContext]] is the Scala API. `getContext` returns a
* [[akka.actor.UntypedActorContext]], which is the Java API of the actor
* context.
*/
implicit val context: ActorContext = { ...