What operations in Java are considered atomic?
Atomic operations are those operations that ALWAYS execute together. Either all of them execute together, or none of them executes. If an operation is atomic, then it cannot be partially complete, either it will be complete, or not start at all, but will not be incomplete.
Variables shared between multiple threads (e.g., instance variables of objects) have atomic assignment guaranteed by the Java language specification for all data types except for long s and double s.
You can think of these are wrapper of primitive types boolean, integer and long, with the difference: they are designed to be safely used in multi-threaded context. They are called atomic variables because they provide some operations that cannot be interfered by multiple threads.
and maybe something more. Look at the jls.
As noted in the comments, atomicity does not imply visibility. So while another thread is guaranteed not to see a partially written int
, it may never see the new value.
The operations on long and double are on common 64 bit CPUs atomic as well, although there's no guarantee. See also this feature request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With