--- jsr166/src/jsr166e/LongAdder.java 2011/08/02 18:04:12 1.5 +++ jsr166/src/jsr166e/LongAdder.java 2014/05/04 22:38:19 1.15 @@ -6,10 +6,7 @@ package jsr166e; import java.util.concurrent.atomic.AtomicLong; -import java.io.IOException; import java.io.Serializable; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; /** * One or more variables that together maintain an initially zero @@ -19,7 +16,7 @@ import java.io.ObjectOutputStream; * #longValue}) returns the current total combined across the * variables maintaining the sum. * - *

This class is usually preferable to {@link AtomicLong} when + *

This class is usually preferable to {@link 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 @@ -28,13 +25,14 @@ import java.io.ObjectOutputStream; * consumption. * *

This class extends {@link Number}, but does not define - * methods such as {@code hashCode} and {@code compareTo} because - * instances are expected to be mutated, and so are not useful as - * collection keys. + * methods such as {@code equals}, {@code hashCode} and {@code + * 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 + * java.util.concurrent.atomic. * + * @since 1.8 * @author Doug Lea */ public class LongAdder extends Striped64 implements Serializable { @@ -57,12 +55,12 @@ public class LongAdder extends Striped64 * @param x the value to add */ public void add(long x) { - Cell[] as; long b, v; HashCode hc; Cell a; int n; + Cell[] as; long b, v; int[] hc; Cell a; int n; if ((as = cells) != null || !casBase(b = base, b + x)) { boolean uncontended = true; - int h = (hc = threadHashCode.get()).code; - if (as == null || (n = as.length) < 1 || - (a = as[(n - 1) & h]) == null || + if ((hc = threadHashCode.get()) == null || + as == null || (n = as.length) < 1 || + (a = as[(n - 1) & hc[0]]) == null || !(uncontended = a.cas(v = a.value, v + x))) retryUpdate(x, hc, uncontended); } @@ -84,7 +82,7 @@ public class LongAdder extends Striped64 /** * Returns the current sum. The returned value is NOT an - * atomic snapshot: Invocation in the absence of concurrent + * 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. @@ -92,8 +90,8 @@ public class LongAdder extends Striped64 * @return the sum */ public long sum() { - Cell[] as = cells; long sum = base; + Cell[] as = cells; if (as != null) { int n = as.length; for (int i = 0; i < n; ++i) { @@ -127,17 +125,16 @@ public class LongAdder extends Striped64 * @return the sum */ public long sumThenReset() { - Cell[] as = cells; long sum = base; + Cell[] as = cells; base = 0L; if (as != null) { int n = as.length; for (int i = 0; i < n; ++i) { Cell a = as[i]; if (a != null) { - long v = a.value; + sum += a.value; a.value = 0L; - sum += v; } } } @@ -146,7 +143,7 @@ public class LongAdder extends Striped64 /** * Returns the String representation of the {@link #sum}. - * @return the String representation of the {@link #sum}. + * @return the String representation of the {@link #sum} */ public String toString() { return Long.toString(sum()); @@ -191,8 +188,8 @@ public class LongAdder extends Striped64 s.writeLong(sum()); } - private void readObject(ObjectInputStream s) - throws IOException, ClassNotFoundException { + private void readObject(java.io.ObjectInputStream s) + throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); busy = 0; cells = null;