5

setGameState() and getGameState() can be called from different threads. m_gameState is volatile so its value/change could be visible to other threads.

Question: Do the functions need to be synchronized or does volatile on the variable suffice?

private volatile EGameState m_gameState;

public void setGameState(EGameState a_gameState) {
   m_gameState = a_gameState;
}

public EGameState getGameState() {
   return m_gameState;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
MicroEyes
  • 3,680
  • 4
  • 26
  • 35
  • Possible duplicate of [Is a write to a volatile a memory-barrier in Java](https://stackoverflow.com/questions/13688697/is-a-write-to-a-volatile-a-memory-barrier-in-java) – Johnny V Feb 23 '19 at 22:18

4 Answers4

2

Access to the volatile variable acts as though it is synchronized on itself.

Accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation.Here you are required to use a synchronized block.

For other cases it will suffice if you didn't use synchronization(like normal get and set)

Vaibhav Gupta
  • 638
  • 4
  • 12
  • Using the phrase "synchronized on itself" does not correctly describe what a volatile does. Synchronization implies mutual coordination when accessed by multiple threads. There is absolutely no guarantee of accurate modification when a volatile is modified by two threads concurrently; only that the last modification becomes the dominating one. This is why compareAndSet is used heavily in the lock mechanics in conjunction with volatile. – Johnny V Feb 23 '19 at 22:35
1

That depends. Do you require the updates of your EGameState field to be ordered or not?
If they must be ordered, than a synchronized block (on this) is mandatory, if not, volatile is sufficient.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
1

If volatile keyword is used with variable, all threads will get latest value of the variable from main memory. Note that it does not acquire a lock so if you want to do any atomic operation, volatile is not sufficient. For atomic operations synchronized is used.In your case you are only reading the value m_gameState = a_gameState; so volatile is sufficient.

Hope it helps.

Vikas
  • 6,868
  • 4
  • 27
  • 41
  • Where do you get `volatile` is used for visibility? `volatile` is an automated Load and Store Fence. Reading from a `volatile` automatically performs a Load Fence and writing automatically performs a Store Fence. This is the reason why it is used often in the lock API. `volatile` is the underlying type of all the Atomic* classes. – Johnny V Feb 23 '19 at 22:05
  • `volatile` keyword guarantees visibility(updated value will be visible to all the threads) of changes to variables across threads. Check: [1]: https://medium.com/@siddhusingh/volatile-visibility-in-jvm-3d2044da017c [2]: http://tutorials.jenkov.com/java-concurrency/volatile.html – Vikas Feb 24 '19 at 02:59
  • 1
    Because `visibility` is ambiguous in your answer and could be mistaken for access-modifiers. JSR-133 is very careful how `visible` is used in the explanation of the memory behaviors of `volatile`. For clarity, you can change your answer to 'memory visibility' or simply remove the first sentence. – Johnny V Feb 25 '19 at 03:26
  • Got your point. Updated the answer. Thanks for your valuable feedback. – Vikas Feb 25 '19 at 05:32