Class 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:
    java.util.function.Consumer<T>, java.util.function.Supplier<R>

    public final class ComplexAccumulator<T,​A,​R>
    extends StripedAccumulator<A>
    implements java.util.function.Consumer<T>, java.util.function.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.

    • Constructor Summary

      Constructor Description
      ComplexAccumulator​(<T,​A,​R> fn)  
    • Method Summary

      All Methods Instance Methods Concrete Methods Default Methods 
      Modifier and Type Method Description
      void accept​(T x)
      Adds the given value.
      R get()
      Returns the current sum.
      R getThenReset()
      Equivalent in effect to get() followed by reset().
      default void pack()  
      void reset()
      Resets variables maintaining the sum to zero.
      java.lang.String toString()
      Returns the String representation of the get().
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.function.Consumer

    • Constructor Detail

      • ComplexAccumulator

        public ComplexAccumulator​(@Nonnull
                        <T,​A,​R> fn)
    • Method Detail

      • accept

        public void accept​(T x)
        Adds the given value.
        Specified by:
        accept in interface java.util.function.Consumer<T>
        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 java.util.function.Supplier<T>
        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.
        the sum
      • toString

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

        public default void pack()