Should my Scala actors' properties be marked @volatile?

后端 未结 1 1788
时光取名叫无心
时光取名叫无心 2021-01-02 19:22

In Scala, if I have a simple class as follows:

val calc = actor {
  var sum = 0
  loop {
    react {
      case Add(n) => 
        sum += n
      case Req         


        
相关标签:
1条回答
  • 2021-01-02 20:20

    You don't need to mark them as volatile. The execution of your code isn't inside a synchronized block, but the actor will always pass through one before your code is invoked, thus forcing memory into a consistent state across threads.

    0 讨论(0)
提交回复
热议问题