Let's say you have this code:
private Object lockObject = new Object();
private Integer myValue = new Integer(0);
public void update(){
synchronized(lockObject){
System.out.println(myValue);
myValue++;
}
}
Now myValueis neither synchronizednor marked volatile. However, the only way to mutate it is to use the method update(). DZone replication on the Java concurrency core states that updates across all fields in a synchronized block are scanned by all threads. I was not sure if this meant only a synchronized object (lockObject) or any field (e.g. myValue).
Can anyone clarify this? Thank!
source
share