Let's say I have the following code:
private Integer number; private final Object numberLock = new Object(); public int get(){ synchronized(number or numberLock){ return Integer.valueOf(number); } }
My question is that the following versions of the add method should have number as mutable in the following cases:
public void add(int num){ synchronized(number) number = number + num; }
public void add(int num){ synchronized(numberLock) number = number + num; }
I understand that these are atomic operations, but my question is that the value of number guarennteed is displayed in the global memory and is visible to all threads without using variability?
Shawn
source share