我阅读了 Akka 文档,但我不明白:
class MyActor extends Actor {
private var _state = 0
override def receive: Receive = {
case x: Int =>
if (x != _state) {
println(s"---------> fail: ${x} and ${_state}")
}
_state = x + 1
}
}
implicit val system = ActorSystem("my-system")
val ref = system.actorOf(Props[MyActor], "my-actor")
(0 to 10000).foreach { x =>
ref ! x
}
如果我使用-method进行更改,我有一个_state
不是@volatile
和不是atomic
但同时_state
始终正确的变量。!
Akka 如何保护和更新 Actor 的内部状态?