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 operation
A - 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:
Consumer<T>, Supplier<R>

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 Details

    • ComplexAccumulator

      public ComplexAccumulator(@Nonnull Collector<T,A,R> fn)
  • Method Details

    • accept

      public void accept(T x)
      Adds the given value.
      Specified by:
      accept in interface 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 interface 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 to get() followed by reset(). 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

      public String toString()
      Returns the String representation of the get().
      Overrides:
      toString in class Object
      Returns:
      the String representation of the get()
    • pack

      default void pack()