Java: should all variable variables be unstable when using locks?

Is the variability of the next variable x required?

Or does the manipulation inside the utils.concurrent lock perform the same function as the synchronized block (ensuring that it is written to memory and not stored in the processor cache)?

myMethod(){ myLock.lock(); x++; myLock.unlock(); } 
+4
source share
1 answer

Such variables must be volatile if they can be accessed elsewhere without blocking. For example, as a read-only quick access to a variable size. Lock methods perform the same task as a synchronized block. See the "Memory sync" section in javadoc for the Lock class.

+4
source

All Articles