Class ComplexAccumulator<T,A,R>
java.lang.Object
com.linkedin.alpini.base.concurrency.StripedAccumulator<A>
com.linkedin.alpini.base.concurrency.ComplexAccumulator<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
public final class ComplexAccumulator<T,A,R>
extends StripedAccumulator<A>
implements Consumer<T>, Supplier<R>
One or more variables that together maintain an accumulation.
When updates (method
accept(T)
) are contended
across threads, the set of variables may grow dynamically to reduce
contention. Method get()
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
-
Constructor Summary
-
Method Summary
-
Constructor Details
-
ComplexAccumulator
-
-
Method Details
-
accept
Adds the given value. -
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. -
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
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
-
toString
Returns the String representation of theget()
. -
pack
default void pack()
-