ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/LongAdder.java
(Generate patch)

Comparing jsr166/src/jsr166e/LongAdder.java (file contents):
Revision 1.5 by dl, Tue Aug 2 18:04:12 2011 UTC vs.
Revision 1.16 by jsr166, Mon May 5 01:57:35 2014 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8   import java.util.concurrent.atomic.AtomicLong;
9 import java.io.IOException;
9   import java.io.Serializable;
11 import java.io.ObjectInputStream;
12 import java.io.ObjectOutputStream;
10  
11   /**
12   * One or more variables that together maintain an initially zero
# Line 19 | Line 16 | import java.io.ObjectOutputStream;
16   * #longValue}) returns the current total combined across the
17   * variables maintaining the sum.
18   *
19 < * <p> This class is usually preferable to {@link AtomicLong} when
19 > * <p>This class is usually preferable to {@link AtomicLong} when
20   * multiple threads update a common sum that is used for purposes such
21   * as collecting statistics, not for fine-grained synchronization
22   * control.  Under low update contention, the two classes have similar
# Line 28 | Line 25 | import java.io.ObjectOutputStream;
25   * consumption.
26   *
27   * <p>This class extends {@link Number}, but does <em>not</em> define
28 < * methods such as {@code hashCode} and {@code compareTo} because
29 < * instances are expected to be mutated, and so are not useful as
30 < * collection keys.
28 > * methods such as {@code equals}, {@code hashCode} and {@code
29 > * compareTo} because instances are expected to be mutated, and so are
30 > * not useful as collection keys.
31   *
32   * <p><em>jsr166e note: This class is targeted to be placed in
33 < * java.util.concurrent.atomic<em>
33 > * java.util.concurrent.atomic.</em>
34   *
35 + * @since 1.8
36   * @author Doug Lea
37   */
38   public class LongAdder extends Striped64 implements Serializable {
# Line 57 | Line 55 | public class LongAdder extends Striped64
55       * @param x the value to add
56       */
57      public void add(long x) {
58 <        Cell[] as; long b, v; HashCode hc; Cell a; int n;
58 >        Cell[] as; long b, v; int[] hc; Cell a; int n;
59          if ((as = cells) != null || !casBase(b = base, b + x)) {
60              boolean uncontended = true;
61 <            int h = (hc = threadHashCode.get()).code;
62 <            if (as == null || (n = as.length) < 1 ||
63 <                (a = as[(n - 1) & h]) == null ||
61 >            if ((hc = threadHashCode.get()) == null ||
62 >                as == null || (n = as.length) < 1 ||
63 >                (a = as[(n - 1) & hc[0]]) == null ||
64                  !(uncontended = a.cas(v = a.value, v + x)))
65                  retryUpdate(x, hc, uncontended);
66          }
# Line 84 | Line 82 | public class LongAdder extends Striped64
82  
83      /**
84       * Returns the current sum.  The returned value is <em>NOT</em> an
85 <     * atomic snapshot: Invocation in the absence of concurrent
85 >     * atomic snapshot; invocation in the absence of concurrent
86       * updates returns an accurate result, but concurrent updates that
87       * occur while the sum is being calculated might not be
88       * incorporated.
# Line 92 | Line 90 | public class LongAdder extends Striped64
90       * @return the sum
91       */
92      public long sum() {
95        Cell[] as = cells;
93          long sum = base;
94 +        Cell[] as = cells;
95          if (as != null) {
96              int n = as.length;
97              for (int i = 0; i < n; ++i) {
# Line 127 | Line 125 | public class LongAdder extends Striped64
125       * @return the sum
126       */
127      public long sumThenReset() {
130        Cell[] as = cells;
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 <                    long v = a.value;
136 >                    sum += a.value;
137                      a.value = 0L;
140                    sum += v;
138                  }
139              }
140          }
# Line 146 | Line 143 | public class LongAdder extends Striped64
143  
144      /**
145       * Returns the String representation of the {@link #sum}.
146 <     * @return the String representation of the {@link #sum}.
146 >     * @return the String representation of the {@link #sum}
147       */
148      public String toString() {
149          return Long.toString(sum());
# Line 191 | Line 188 | public class LongAdder extends Striped64
188          s.writeLong(sum());
189      }
190  
191 <    private void readObject(ObjectInputStream s)
192 <        throws IOException, ClassNotFoundException {
191 >    private void readObject(java.io.ObjectInputStream s)
192 >        throws java.io.IOException, ClassNotFoundException {
193          s.defaultReadObject();
194          busy = 0;
195          cells = null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines