AtomicLong
Explore AtomicLong in Java concurrency to understand how it provides atomic operations for long values using compare-and-swap instructions. Learn its performance benefits versus traditional locking, how it differs from the long type, and methods to simulate atomic double values for thread-safe programming.
We'll cover the following...
If you are interviewing, consider buying our number#1 course for Java Multithreading Interviews.
Overview
AtomicLong is the equivalent class for long type in the java.util.concurrent.atomic package as is AtomicInteger for int type. The AtomicLong class represents a long value that can be updated atomically, i.e. the read-modify-write operation can be executed atomically upon an instance of AtomicLong. The class extends Number.
Like the AtomicInteger class the AtomicLong` makes for great counters, sequence numbers etc as it uses the compare-and-swap (CAS) instruction under the hood. The CAS instruction doesn’t penalize competing threads for access to shared data/state with suspension as locks do. In general, suspension and resumption of threads involves significant overhead and under low to moderate contention non-blocking algorithms that use CAS outperform lock-based alternatives.
Performance
To demonstrate the performance of AtomicLong we can construct a crude test, where a counter is incremented a million times by ten threads to reach a total of ten million. We’ll time the run for an AtomicLong counter and an ordinary long counter. The widget below outputs the results:
Difference with long
Remember that AtomicLong isn’t a drop-in replacement for long. Specifically, just like the AtomicInteger class, the AtomicLong doesn’t override equals() or hashcode() and each instance is distinct. The widget below demonstrates that the same long value for two different AtomicLong instances doesn’t hash to the same bucket.
Using AtomicLong to simulate atomic double
Previously, we demonstrated how the AtomicInteger class can be used to create atomic byte and atomic float types. Similarly, an equivalent atomic class for double primitive type doesn’t exist, however, we can simulate one using AtomicLong as we did for the primitive type byte. The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion.This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.
To create our custom AtomicDobule type, we extend from the class Number and override several of its functions. Under the hood, we save the floating point’s bit representation in an instance of AtomicLong. The class listing appears below with comments for explanation:
The AtomicDouble class in this lesson is shown for instructional purposes only. If you need to use this primitive type atomically, you can find well-written and well-tested libraries on the internet. For instance, Google’s Guava library offers an AtomicDouble type.