CKTIS / Java
LongAccumulator: construction, updates, and reading a running maximum
Track a running `long` maximum with `LongAccumulator`, choose an identity that works for your input range, and understand what `get()` can report during concurrent updates.
Version: Java SE 21
Syntax
import java.util.concurrent.atomic.LongAccumulator;
import java.util.function.LongBinaryOperator;
// API signatures
public LongAccumulator(LongBinaryOperator accumulatorFunction, long identity)
public void accumulate(long x)
public long get()
// Running maximum over the full long range
LongAccumulator maximum = new LongAccumulator(Long::max, Long.MIN_VALUE);
maximum.accumulate(-7L);
long currentMaximum = maximum.get();In Java SE 21, the LongAccumulator(LongBinaryOperator,long) constructor in java.util.concurrent.atomic takes an operator and its identity, which is also the initial value. A java.util.function.LongBinaryOperator takes two primitive long values and returns a long. An identity leaves an input unchanged: f(identity, x) == x.
LongAccumulator.accumulate/get refers to two separate calls: accumulate(long x) incorporates x using the operator and returns nothing (void); get() combines the partial results and returns a long without clearing the state. The accumulator may keep separate partial results to reduce contention. Accumulation order is not guaranteed, so use an associative, commutative operator. Keep it free of side effects: contention can cause it to be called again.
The method reference Long::max selects the larger of two values. For the full long range, use Long.MIN_VALUE because max(Long.MIN_VALUE, x) == x. The value 0L is neutral only for nonnegative inputs such as sizes. If inputs can be negative, 0L is not an identity, and Java SE 21 does not guarantee the result if you supply it as one. Before any update, get() returns the identity, so Long.MIN_VALUE alone cannot distinguish no inputs from a real maximum equal to that value.
Without concurrent updates, get() returns an accurate result. During updates, it is not an atomic snapshot and may omit some updates. Use this for statistics, not for thread coordination. The separate reset() method restores the identity; call it only when no thread is updating the accumulator.
Examples
import java.util.concurrent.atomic.LongAccumulator;
public class Main {
public static void main(String[] args) {
LongAccumulator fullRange = new LongAccumulator(Long::max, Long.MIN_VALUE);
LongAccumulator withZero = new LongAccumulator(Long::max, Long.MIN_VALUE);
withZero.accumulate(0L);
for (long value : new long[] {-7L, -3L, -12L}) {
fullRange.accumulate(value);
withZero.accumulate(value);
}
System.out.println(fullRange.get());
System.out.println(fullRange.get());
System.out.println(withZero.get());
}
}Result
-3 -3 0
Two reads of the same accumulator give `-3` because `get()` does not reset the state. The second accumulator returns `0` because it also receives `0L` as an input. Both accumulators use `Long.MIN_VALUE` as the identity.
Related lessons
- Java · Lesson 31 · Advanced · Version: 2