Class ConcurrentAccumulator<T,A,R>
- java.lang.Object
-
- com.linkedin.alpini.base.concurrency.ConcurrentAccumulator<T,A,R>
-
- Type Parameters:
T
- the type of input elements to the reduction operationA
- the mutable accumulation type of the reduction operation (often hidden as an implementation detail)R
- the result type of the reduction operation
- All Implemented Interfaces:
java.util.function.Consumer<T>
,java.util.function.Supplier<R>
public final class ConcurrentAccumulator<T,A,R> extends java.lang.Object implements java.util.function.Consumer<T>, java.util.function.Supplier<R>
One or more variables that together maintain an accumulation. When updates (methodaccept(T)
) are contended across threads, the set of variables may grow dynamically to reduce contention. Methodget()
returns the current total combined across the variables maintaining the accumulation.This class is usually preferable to
AtomicReference
when multiple threads update a common object that is used for purposes such as collecting statistics, not for fine-grained synchronization control. Under low update contention, the two classes have similar characteristics. But under high contention, expected throughput of this class is significantly higher, at the expense of higher space consumption.- Since:
- 1.8
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ConcurrentAccumulator.Mode
-
Field Summary
Fields Modifier and Type Field Description static ConcurrentAccumulator.Mode
defaultMode
-
Constructor Summary
Constructors Constructor Description ConcurrentAccumulator(ConcurrentAccumulator.Mode mode, java.util.stream.Collector<T,A,R> fn)
ConcurrentAccumulator(java.util.stream.Collector<T,A,R> fn)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(T x)
Adds the given value.R
get()
Returns the current sum.R
getThenReset()
void
pack()
void
reset()
Resets variables maintaining the sum to zero.java.lang.String
toString()
Returns the String representation of theget()
.
-
-
-
Field Detail
-
defaultMode
public static ConcurrentAccumulator.Mode defaultMode
-
-
Method Detail
-
accept
public void accept(T x)
Adds the given value.- Specified by:
accept
in interfacejava.util.function.Consumer<T>
- Parameters:
x
- the value to add
-
get
public R get()
Returns the current sum. The returned value is NOT an atomic snapshot; invocation in the absence of concurrent updates returns an accurate result, but concurrent updates that occur while the sum is being calculated might not be incorporated.- Specified by:
get
in interfacejava.util.function.Supplier<T>
- Returns:
- the sum
-
reset
public void reset()
Resets variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy, it should only be used when it is known that no threads are concurrently updating.
-
getThenReset
public R getThenReset()
Equivalent in effect toget()
followed byreset()
. This method may apply for example during quiescent points between multithreaded computations. If there are updates concurrent with this method, the returned value is not guaranteed to be the final value occurring before the reset.- Returns:
- the sum
-
pack
public void pack()
-
-