Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Written by Doug Lea with assistance from members of JCP JSR-166
   * Expert Group and released to the public domain, as explained at
   * http://creativecommons.org/publicdomain/zero/1.0/
   */
  
  /*
   * Source:
   * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/LongAdder.java?revision=1.17
  */
 
 package com.google.common.cache;
 
 
One or more variables that together maintain an initially zero long sum. When updates (method add(long)) are contended across threads, the set of variables may grow dynamically to reduce contention. Method sum() (or, equivalently, longValue()) returns the current total combined across the variables maintaining the sum.

This class is usually preferable to java.util.concurrent.atomic.AtomicLong when multiple threads update a common sum 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.

This class extends java.lang.Number, but does not define methods such as equals, hashCode and compareTo because instances are expected to be mutated, and so are not useful as collection keys.

jsr166e note: This class is targeted to be placed in java.util.concurrent.atomic.

Author(s):
Doug Lea
Since:
1.8
 
 @GwtCompatible(emulated = true)
 final class LongAdder extends Striped64 implements SerializableLongAddable {
     private static final long serialVersionUID = 7249069246863182397L;

    
Version of plus for use in retryUpdate
 
     final long fn(long vlong x) { return v + x; }

    
Creates a new adder with initial sum of zero.
 
     public LongAdder() {
     }

    
Adds the given value.

Parameters:
x the value to add
 
     public void add(long x) {
         Cell[] aslong bvint[] hcCell aint n;
         if ((as = ) != null || !casBase(b = b + x)) {
             boolean uncontended = true;
             if ((hc = .get()) == null ||
                 as == null || (n = as.length) < 1 ||
                 (a = as[(n - 1) & hc[0]]) == null ||
                 !(uncontended = a.cas(v = a.valuev + x)))
                 retryUpdate(xhcuncontended);
         }
     }

    
Equivalent to add(1).
 
     public void increment() {
         add(1L);
     }

    
Equivalent to add(-1).
 
     public void decrement() {
         add(-1L);
     }

    
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.

Returns:
the sum
    public long sum() {
        long sum = ;
        Cell[] as = ;
        if (as != null) {
            int n = as.length;
            for (int i = 0; i < n; ++i) {
                Cell a = as[i];
                if (a != null)
                    sum += a.value;
            }
        }
        return sum;
    }

    
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.
    public void reset() {
        internalReset(0L);
    }

    
Equivalent in effect to sum() 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
    public long sumThenReset() {
        long sum = ;
        Cell[] as = ;
         = 0L;
        if (as != null) {
            int n = as.length;
            for (int i = 0; i < n; ++i) {
                Cell a = as[i];
                if (a != null) {
                    sum += a.value;
                    a.value = 0L;
                }
            }
        }
        return sum;
    }

    
Returns the String representation of the sum().

Returns:
the String representation of the sum()
    public String toString() {
        return Long.toString(sum());
    }

    
Equivalent to sum().

Returns:
the sum
    public long longValue() {
        return sum();
    }

    
Returns the sum() as an int after a narrowing primitive conversion.
    public int intValue() {
        return (int)sum();
    }

    
Returns the sum() as a float after a widening primitive conversion.
    public float floatValue() {
        return (float)sum();
    }

    
Returns the sum() as a double after a widening primitive conversion.
    public double doubleValue() {
        return (double)sum();
    }
    private void writeObject(ObjectOutputStream sthrows IOException {
        s.defaultWriteObject();
        s.writeLong(sum());
    }
    private void readObject(ObjectInputStream s)
            throws IOExceptionClassNotFoundException {
        s.defaultReadObject();
         = 0;
         = null;
         = s.readLong();
    }
New to GrepCode? Check out our FAQ X