Rajesh Nagaraju wrote:volatile for long and double are treated as atomic, if in the 16 bit processor still we cannot read it in 1 operation..
How does the JVM manage to maintain it atomic?
Because our Java code doesn't run on the hardware CPU. It runs in the JVM. Even in a 32-bit processor, doubles and longs are two words. The JVM controls the execution of our instructions, so it can make sure that our instructions always see certain operations as atomic, even if the hardware doesn't support that. Just like we can used the synchronized keyword to make long, complex operations atomic in our code, so can the JVM use the underlying native API's multithreading features to make its operations atomic.
At the bare minimum, all that is required is that the hardware provides an atomic test-and-set operation, or some equivalent mutual exclusion feature. If that's present, anything can be made atomic. If it's not, nothing can.