atomic
Categories:
In programming, an atomic action is one that effectively happens all at once.
it either happens completely, or it doesn’t happen at all.
some method you can consider as atomic
-
Reads and writes are atomic for reference variables and for most primitive variables (all types except
long
anddouble
). -
Reads and writes are atomic for all variables declared
volatile
Atomic actions cannot be interleaved, so they can be used without fear of thread interference.
volatile
idiom
Reads and writes are atomic for all variables declared volatile
(include double and long)
In most 32-bit and 64-bit processors, a
long
ordouble
variable requires two separate 32-bit reads or writes to access the full 64-bit value.As a result, the operation of reading or writing a
long
ordouble
is not guaranteed to be atomic at the hardware level.
Using volatile
can reduce the risk of mem-consist, because any write to volatile var will establishes a happen-before relationship with subsequent reads of that same variable
This means that changes to a volatile
variable are always visible to other threads.
Using simple atomic variable access is more efficient than accessing these variables through synchronized code