tlarevo
tlarevo

Reputation: 635

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

In Scala singletons are created using Object classes and when serval Akka actors call methods in particular Object, would race condition occur ?

Upvotes: 0

Views: 304

Answers (2)

Agree with, S V's answer. A convenient "guard" for values is to use Agent. Reads are immediate, writes are asynchronous:

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)}

Upvotes: 1

S V
S V

Reputation: 580

If the object holds state, then yes. It will require proper synchronization guards

Upvotes: 5

Related Questions