5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166e; |
8 |
< |
import java.util.concurrent.atomic.AtomicLong; |
8 |
> |
|
9 |
|
import java.io.IOException; |
10 |
– |
import java.io.Serializable; |
10 |
|
import java.io.ObjectInputStream; |
11 |
|
import java.io.ObjectOutputStream; |
12 |
+ |
import java.io.Serializable; |
13 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
14 |
|
|
15 |
|
/** |
16 |
|
* One or more variables that together maintain an initially zero |
20 |
|
* #longValue}) returns the current total combined across the |
21 |
|
* variables maintaining the sum. |
22 |
|
* |
23 |
< |
* <p> This class is usually preferable to {@link AtomicLong} when |
23 |
> |
* <p>This class is usually preferable to {@link AtomicLong} when |
24 |
|
* multiple threads update a common sum that is used for purposes such |
25 |
|
* as collecting statistics, not for fine-grained synchronization |
26 |
|
* control. Under low update contention, the two classes have similar |
29 |
|
* consumption. |
30 |
|
* |
31 |
|
* <p>This class extends {@link Number}, but does <em>not</em> define |
32 |
< |
* methods such as {@code hashCode} and {@code compareTo} because |
33 |
< |
* instances are expected to be mutated, and so are not useful as |
34 |
< |
* collection keys. |
32 |
> |
* methods such as {@code equals}, {@code hashCode} and {@code |
33 |
> |
* compareTo} because instances are expected to be mutated, and so are |
34 |
> |
* not useful as collection keys. |
35 |
|
* |
36 |
|
* <p><em>jsr166e note: This class is targeted to be placed in |
37 |
< |
* java.util.concurrent.atomic<em> |
37 |
> |
* java.util.concurrent.atomic.</em> |
38 |
|
* |
39 |
+ |
* @since 1.8 |
40 |
|
* @author Doug Lea |
41 |
|
*/ |
42 |
|
public class LongAdder extends Striped64 implements Serializable { |
59 |
|
* @param x the value to add |
60 |
|
*/ |
61 |
|
public void add(long x) { |
62 |
< |
Cell[] as; long b, v; HashCode hc; Cell a; int n; |
62 |
> |
Cell[] as; long b, v; int[] hc; Cell a; int n; |
63 |
|
if ((as = cells) != null || !casBase(b = base, b + x)) { |
64 |
|
boolean uncontended = true; |
65 |
< |
int h = (hc = threadHashCode.get()).code; |
66 |
< |
if (as == null || (n = as.length) < 1 || |
67 |
< |
(a = as[(n - 1) & h]) == null || |
65 |
> |
if ((hc = threadHashCode.get()) == null || |
66 |
> |
as == null || (n = as.length) < 1 || |
67 |
> |
(a = as[(n - 1) & hc[0]]) == null || |
68 |
|
!(uncontended = a.cas(v = a.value, v + x))) |
69 |
|
retryUpdate(x, hc, uncontended); |
70 |
|
} |
86 |
|
|
87 |
|
/** |
88 |
|
* Returns the current sum. The returned value is <em>NOT</em> an |
89 |
< |
* atomic snapshot: Invocation in the absence of concurrent |
89 |
> |
* atomic snapshot; invocation in the absence of concurrent |
90 |
|
* updates returns an accurate result, but concurrent updates that |
91 |
|
* occur while the sum is being calculated might not be |
92 |
|
* incorporated. |
147 |
|
|
148 |
|
/** |
149 |
|
* Returns the String representation of the {@link #sum}. |
150 |
< |
* @return the String representation of the {@link #sum}. |
150 |
> |
* @return the String representation of the {@link #sum} |
151 |
|
*/ |
152 |
|
public String toString() { |
153 |
|
return Long.toString(sum()); |
186 |
|
return (double)sum(); |
187 |
|
} |
188 |
|
|
189 |
< |
private void writeObject(java.io.ObjectOutputStream s) |
188 |
< |
throws java.io.IOException { |
189 |
> |
private void writeObject(ObjectOutputStream s) throws IOException { |
190 |
|
s.defaultWriteObject(); |
191 |
|
s.writeLong(sum()); |
192 |
|
} |
193 |
|
|
194 |
|
private void readObject(ObjectInputStream s) |
195 |
< |
throws IOException, ClassNotFoundException { |
195 |
> |
throws IOException, ClassNotFoundException { |
196 |
|
s.defaultReadObject(); |
197 |
|
busy = 0; |
198 |
|
cells = null; |