The java memory model mandates that writing a int
is atomic: That is, if you write a value to it (consisting of 4 bytes) in one thread and read it in another, you will get all bytes or none, but never 2 new bytes and 2 old bytes or such.
This is not guaranteed for long
. Here, writing 0x1122334455667788
to a variable holding 0
before could result in another thread reading 0x112233440000000
or 0x0000000055667788
.
Now the specification does not mandate object references to be either int or long-sized. For type safety reasons I suspect they are guaranteed to be written atomically, but on a 64bit VM these references could be very well 64bit values (merely memory addresses).
Now here are my questions:
- Are there any memory model specs covering this (that I haven't found)?
- Are long-writes suspect to be atomic on 64bit VMs?
- Are VMs forced to map references to 32bit?
Regards,
Steffen
Best Answer
Reading/writing references always atomic
See JLS section 17.7: Non-atomic Treatment of double and long
(Emphasis added)
AtomicReference
If you want to coordinate between old and new values, or want specific memory effects, use the class
AtomicReference
.For example,
AtomicReference::getAndSet
returns the old value while setting the new value atomically, eliminating any chance of another thread having intervened between the two steps. Usesvolatile
memory semantics.