Java Concurrency Interview

The Java memory model question 21 of 180 mid

Is long and double assignment atomic in Java?

The JLS explicitly permits a non-volatile 64-bit write to be treated as two 32-bit writes, so a reader can observe the high half of one value with the low half of another: a value that was never written by anyone. Declaring the field volatile makes the access atomic and the tearing disappears.

In practice every 64-bit HotSpot build writes a long atomically, so this is difficult to reproduce and often dismissed. It is still the right answer to the question as asked, and it is the honest reason AtomicLong exists as a type rather than as a convenience wrapper.

memory-model atomicity

Quiz yourself on The Java memory model Suggest a correction