LongAdder¶
LongAdder is a class introduced in Java 8 as part of the java.util.concurrent atomic package, designed for high-throughput scenarios involving frequent increments.^[600-developer-java-java-base-java-runtime-number.md] It is implemented using the Striped64 framework, a concurrent component added in Java 8 to support accumulators in concurrent environments for counting purposes.^[600-developer-java-java-base-java-runtime-number.md]
Functionality¶
The primary use case for LongAdder is as a counter in multi-threaded applications.^[600-developer-java-java-base-java-runtime-number.md] For example, it can be used to track statistics across a thread pool where threads perform a specific number of increment operations.^[600-developer-java-java-base-java-runtime-number.md]
Relationship with LongAccumulator¶
LongAdder is functionally equivalent to a specific configuration of LongAccumulator.^[600-developer-java-java-base-java-runtime-number.md] Specifically, the call new LongAdder() is equivalent to new [LongAccumulator](<./longaccumulator.md>)((x, y) -> x + y, 0L).^[600-developer-java-java-base-java-runtime-number.md] Similarly, the DoubleAdder class is equivalent to new [LongAccumulator](<./longaccumulator.md>)((x, y) -> x + y, 0.0).^[600-developer-java-java-base-java-runtime-number.md]
Related Concepts¶
- LongAccumulator
- Striped64
- [[Atomic Variables]]
Sources¶
600-developer-java-java-base-java-runtime-number.md