More on Atomics
Learn about the different categories of atomic classes and their performance in comparison to locks.
Taxonomy of atomic classes
There are a total of sixteen atomic classes divided into four groups:
- Scalars
- Field updaters
- Arrays
- Compound variables
Most well-known and commonly used are the scalar ones such as AtomicInteger
, AtomicLong
, AtomicReference
, which support the CAS (compare-and-set). Other primitive types such as double and float can be simulated by casting short
or byte
values to and from int
and using methods floatToIntBits()
and doubleToLongBits()
for floating point numbers. Atomic scalar classes extend from Number
and don’t redefine hashCode()
or equals()
.
Atomics are not primitvies
The following widget highlights these differences between Integer
and AtomicInteger
. Note, that the Integer
class has the same hashcode for the same integer value but that’s not the case for AtomicInteger
. Thus Atomic*
scalar classes are unsuitable as keys for collections that rely on hashcode.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.