One or more variables that together maintain an initially zero long sum. When updates (method #add) are contended across threads, the set of variables may grow dynamically to reduce contention. Method #sum (or, equivalently, #longValue) returns the current total comb
| 47 | * @author Doug Lea |
| 48 | */ |
| 49 | @GwtCompatible(emulated = true) |
| 50 | final class LongAdder extends Striped64 implements Serializable, LongAddable { |
| 51 | private static final long serialVersionUID = 7249069246863182397L; |
| 52 | |
| 53 | /** |
| 54 | * Version of plus for use in retryUpdate |
| 55 | */ |
| 56 | final long fn(long v, long x) { return v + x; } |
| 57 | |
| 58 | /** |
| 59 | * Creates a new adder with initial sum of zero. |
| 60 | */ |
| 61 | public LongAdder() { |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Adds the given value. |
| 66 | * |
| 67 | * @param x the value to add |
| 68 | */ |
| 69 | public void add(long x) { |
| 70 | Cell[] as; long b, v; int[] hc; Cell a; int n; |
| 71 | if ((as = cells) != null || !casBase(b = base, b + x)) { |
| 72 | boolean uncontended = true; |
| 73 | if ((hc = threadHashCode.get()) == null || |
| 74 | as == null || (n = as.length) < 1 || |
| 75 | (a = as[(n - 1) & hc[0]]) == null || |
| 76 | !(uncontended = a.cas(v = a.value, v + x))) |
| 77 | retryUpdate(x, hc, uncontended); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Equivalent to {@code add(1)}. |
| 83 | */ |
| 84 | public void increment() { |
| 85 | add(1L); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Equivalent to {@code add(-1)}. |
| 90 | */ |
| 91 | public void decrement() { |
| 92 | add(-1L); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the current sum. The returned value is <em>NOT</em> an |
| 97 | * atomic snapshot; invocation in the absence of concurrent |
| 98 | * updates returns an accurate result, but concurrent updates that |
| 99 | * occur while the sum is being calculated might not be |
| 100 | * incorporated. |
| 101 | * |
| 102 | * @return the sum |
| 103 | */ |
| 104 | public long sum() { |
| 105 | long sum = base; |
| 106 | Cell[] as = cells; |
nothing calls this directly
no outgoing calls
no test coverage detected