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