It looks like you are referring to this section of JLS. It is guaranteed for all primitive types - except for double
and long
- that all threads will see some value that was actually written to this variable. (With double
and long
first four bytes could be written by one stream, and the last four bytes by another stream, as indicated in this JLS section.) But they will not necessarily see the same value at the same time if the variable is not marked volatile
.
Even using volatile
, x += 3
not atomic, because it is x = x + 3
, which reads and writes, and can be written to x
between read and write. This is why we have things like AtomicInteger
and other utilities in java.util.concurrent
.
source share