当 Scala 中有多个 Actors access/call Object class 方法时会出现竞争条件吗?

Would there be a race condition when multiple Actors access/call Object class methods in Scala?

在 Scala 中,单例是使用 Object 类 创建的,当 serval Akka actors 调用方法时,特别是 Object,会竞争情况发生?

如果对象保持状态,那么是的。它将需要适当的同步保护

同意,S V 的回答。一个方便的 "guard" 值是使用 Agent。读取是即时的,写入是异步的:

import scala.concurrent.ExecutionContext.Implicits.global
import akka.agent.Agent

val agent = Agent(5)

Future {agent send 12}
Future { agent send (_ + 4)}
Future { agent send (_ * 2)}