64-bit types and atomicity
Some implementation may find this convenient to divide a single write action onto a 64-bit long or double value into two write action on adjacent 32-bit values. For efficient sake, this behavior is implementation specific
JVM’s are free to perform write to long and double values atomically or in 2 parts. From Java Memory Model point of view a single write to a non-volatile long or double value is treated as two separate write: one for each 32-bits half. This can result into situation where threads see the first 32-bits of a 64-bit value from one write and the second 32-bit from another write.
- Write to a volatile long and double values are always atomic.
- Write to and read of a reference are always atomic; regardless of whether they are implemented as 32-bit or 64-bit
declare shared 64-bit values as volatile or synchronize their read/write.
Further reading