Coverage Summary for Class: LongAdder (com.google.common.cache)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| LongAdder | 0% (0/1) | 0% (0/15) | 0% (0/39) |
1 /* 2 * Written by Doug Lea with assistance from members of JCP JSR-166 3 * Expert Group and released to the public domain, as explained at 4 * http://creativecommons.org/publicdomain/zero/1.0/ 5 */ 6 7 /* 8 * Source: 9 * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/LongAdder.java?revision=1.17 10 */ 11 12 package com.google.common.cache; 13 14 import com.google.common.annotations.GwtCompatible; 15 import java.io.IOException; 16 import java.io.ObjectInputStream; 17 import java.io.ObjectOutputStream; 18 import java.io.Serializable; 19 import java.util.concurrent.atomic.AtomicLong; 20 21 /** 22 * One or more variables that together maintain an initially zero {@code long} sum. When updates 23 * (method {@link #add}) are contended across threads, the set of variables may grow dynamically to 24 * reduce contention. Method {@link #sum} (or, equivalently, {@link #longValue}) returns the current 25 * total combined across the variables maintaining the sum. 26 * 27 * <p>This class is usually preferable to {@link AtomicLong} when multiple threads update a common 28 * sum that is used for purposes such as collecting statistics, not for fine-grained synchronization 29 * control. Under low update contention, the two classes have similar characteristics. But under 30 * high contention, expected throughput of this class is significantly higher, at the expense of 31 * higher space consumption. 32 * 33 * <p>This class extends {@link Number}, but does <em>not</em> define methods such as {@code 34 * equals}, {@code hashCode} and {@code compareTo} because instances are expected to be mutated, and 35 * so are not useful as collection keys. 36 * 37 * <p><em>jsr166e note: This class is targeted to be placed in java.util.concurrent.atomic.</em> 38 * 39 * @since 1.8 40 * @author Doug Lea 41 */ 42 @GwtCompatible(emulated = true) 43 final class LongAdder extends Striped64 implements Serializable, LongAddable { 44 private static final long serialVersionUID = 7249069246863182397L; 45 46 /** Version of plus for use in retryUpdate */ 47 @Override 48 final long fn(long v, long x) { 49 return v + x; 50 } 51 52 /** Creates a new adder with initial sum of zero. */ 53 public LongAdder() {} 54 55 /** 56 * Adds the given value. 57 * 58 * @param x the value to add 59 */ 60 @Override 61 public void add(long x) { 62 Cell[] as; 63 long b, v; 64 int[] hc; 65 Cell a; 66 int n; 67 if ((as = cells) != null || !casBase(b = base, b + x)) { 68 boolean uncontended = true; 69 if ((hc = threadHashCode.get()) == null 70 || as == null 71 || (n = as.length) < 1 72 || (a = as[(n - 1) & hc[0]]) == null 73 || !(uncontended = a.cas(v = a.value, v + x))) retryUpdate(x, hc, uncontended); 74 } 75 } 76 77 /** Equivalent to {@code add(1)}. */ 78 @Override 79 public void increment() { 80 add(1L); 81 } 82 83 /** Equivalent to {@code add(-1)}. */ 84 public void decrement() { 85 add(-1L); 86 } 87 88 /** 89 * Returns the current sum. The returned value is <em>NOT</em> an atomic snapshot; invocation in 90 * the absence of concurrent updates returns an accurate result, but concurrent updates that occur 91 * while the sum is being calculated might not be incorporated. 92 * 93 * @return the sum 94 */ 95 @Override 96 public long sum() { 97 long sum = base; 98 Cell[] as = cells; 99 if (as != null) { 100 int n = as.length; 101 for (int i = 0; i < n; ++i) { 102 Cell a = as[i]; 103 if (a != null) sum += a.value; 104 } 105 } 106 return sum; 107 } 108 109 /** 110 * Resets variables maintaining the sum to zero. This method may be a useful alternative to 111 * creating a new adder, but is only effective if there are no concurrent updates. Because this 112 * method is intrinsically racy, it should only be used when it is known that no threads are 113 * concurrently updating. 114 */ 115 public void reset() { 116 internalReset(0L); 117 } 118 119 /** 120 * Equivalent in effect to {@link #sum} followed by {@link #reset}. This method may apply for 121 * example during quiescent points between multithreaded computations. If there are updates 122 * concurrent with this method, the returned value is <em>not</em> guaranteed to be the final 123 * value occurring before the reset. 124 * 125 * @return the sum 126 */ 127 public long sumThenReset() { 128 long sum = base; 129 Cell[] as = cells; 130 base = 0L; 131 if (as != null) { 132 int n = as.length; 133 for (int i = 0; i < n; ++i) { 134 Cell a = as[i]; 135 if (a != null) { 136 sum += a.value; 137 a.value = 0L; 138 } 139 } 140 } 141 return sum; 142 } 143 144 /** 145 * Returns the String representation of the {@link #sum}. 146 * 147 * @return the String representation of the {@link #sum} 148 */ 149 @Override 150 public String toString() { 151 return Long.toString(sum()); 152 } 153 154 /** 155 * Equivalent to {@link #sum}. 156 * 157 * @return the sum 158 */ 159 @Override 160 public long longValue() { 161 return sum(); 162 } 163 164 /** Returns the {@link #sum} as an {@code int} after a narrowing primitive conversion. */ 165 @Override 166 public int intValue() { 167 return (int) sum(); 168 } 169 170 /** Returns the {@link #sum} as a {@code float} after a widening primitive conversion. */ 171 @Override 172 public float floatValue() { 173 return (float) sum(); 174 } 175 176 /** Returns the {@link #sum} as a {@code double} after a widening primitive conversion. */ 177 @Override 178 public double doubleValue() { 179 return (double) sum(); 180 } 181 182 private void writeObject(ObjectOutputStream s) throws IOException { 183 s.defaultWriteObject(); 184 s.writeLong(sum()); 185 } 186 187 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { 188 s.defaultReadObject(); 189 busy = 0; 190 cells = null; 191 base = s.readLong(); 192 } 193 }