Java Atomic Package Classes: AtomicBoolean, AtomicInteger, AtomicLong, and LongAdder
This article introduces Java's java.util.concurrent.atomic package, detailing the core atomic classes—AtomicBoolean, AtomicInteger, AtomicLong, and LongAdder—including their constructors, key methods, typical use cases, and performance considerations in high‑concurrency scenarios for developers seeking efficient thread‑safe operations.
2.4 Atomic Package Classes
The java.util.concurrent.atomic package provides a set of classes for implementing atomic operations, enabling thread‑safe, non‑blocking actions in multithreaded environments without using explicit locks. These classes excel in high‑concurrency scenarios where performance is critical.
Four atomic classes are commonly used in performance testing: AtomicBoolean , AtomicInteger , AtomicLong , and LongAdder .
The first three correspond to Java primitive types and replace operators with method calls for getting and setting values. The following sections introduce each class.
1. AtomicBoolean
AtomicBoolean is used for atomic operations on boolean values in multithreaded code, allowing direct modification without locks or synchronized . Common methods include:
public AtomicBoolean(boolean initialValue) : constructor; default value is false when no argument is provided.
get() : returns the current boolean value.
set(boolean newValue) : sets a new boolean value.
getAndSet(boolean newValue) : returns the current value and then sets a new one.
compareAndSet(boolean expect, boolean update) : if the current value equals expect , updates it to update and returns true ; otherwise returns false .
In performance tests, AtomicBoolean is often used to mark thread states, control start/stop, flag resource initialization, and limit the number of concurrently executing operations.
2. AtomicInteger
AtomicInteger provides atomic operations for integer values, supporting thread‑safe increment/decrement without locks. Key methods include:
public AtomicInteger(int initialValue) : constructor; default value is 0 when omitted.
get() : returns the current integer.
set(int newValue) : sets a new integer value.
getAndSet(int newValue) : returns the current value and then sets a new one.
getAndIncrement() / getAndDecrement() : returns the current value and then increments or decrements it (post‑operation).
incrementAndGet() / decrementAndGet() : increments or decrements first, then returns the new value (pre‑operation).
addAndGet(int delta) : adds delta (which may be negative) to the current value and returns the result.
compareAndSet(int expect, int update) : atomically sets the value to update if the current value equals expect .
AtomicInteger is typically used for counters and state control in multithreaded scenarios, such as counting executed tasks or aggregating monetary totals.
3. AtomicLong
AtomicLong offers the same functionality as AtomicInteger but for the long primitive, supporting a wider numeric range.
4. LongAdder
LongAdder provides the same API as AtomicLong and AtomicInteger but achieves better performance under high contention by using segmented counters (similar to ConcurrentHashMap ). Common methods include:
LongAdder() : constructor; initial value is 0 .
add(long x) : adds x (which may be negative) to the current sum.
increment() / decrement() : equivalent to add(1L) and add(-1L) .
sum() : returns the current accumulated value.
reset() : resets the counter to 0 .
sumThenReset() : returns the current sum and then resets to 0 .
Use LongAdder when you need high‑throughput counting or large‑scale data aggregation; it outperforms the other atomic classes in scenarios with 500+ concurrent threads, shows slight advantage at 200‑300 threads, and is comparable at lower concurrency.
If you are unsure which class to choose, the Java micro‑benchmark harness (JMH) can help you make an informed decision based on real‑world measurements.
Book title: "From Java Start Performance Testing".
If the book helps you, consider a modest donation to support the author; a two‑digit contribution grants early access to unpublished chapters, and the author plans to produce video tutorials with Q&A.
FunTester Original Highlights [Series] From Java Start Performance Testing Chaos Engineering, Fault Testing, Web Frontend Server-side Functional Testing Performance Testing Topics Java, Groovy, Go White‑box Testing, Tools, Crawlers, UI Automation Theory, Insights, Video
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.