5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166e; |
8 |
+ |
|
9 |
|
import java.io.IOException; |
9 |
– |
import java.io.Serializable; |
10 |
|
import java.io.ObjectInputStream; |
11 |
|
import java.io.ObjectOutputStream; |
12 |
+ |
import java.io.Serializable; |
13 |
|
|
14 |
|
/** |
15 |
|
* One or more variables that together maintain an initially zero |
20 |
|
* variables maintaining the sum. |
21 |
|
* |
22 |
|
* <p>This class extends {@link Number}, but does <em>not</em> define |
23 |
< |
* methods such as {@code hashCode} and {@code compareTo} because |
24 |
< |
* instances are expected to be mutated, and so are not useful as |
25 |
< |
* collection keys. |
23 |
> |
* methods such as {@code equals}, {@code hashCode} and {@code |
24 |
> |
* compareTo} because instances are expected to be mutated, and so are |
25 |
> |
* not useful as collection keys. |
26 |
|
* |
27 |
|
* <p><em>jsr166e note: This class is targeted to be placed in |
28 |
< |
* java.util.concurrent.atomic<em> |
28 |
> |
* java.util.concurrent.atomic.</em> |
29 |
|
* |
30 |
|
* @since 1.8 |
31 |
|
* @author Doug Lea |
63 |
|
* @param x the value to add |
64 |
|
*/ |
65 |
|
public void add(double x) { |
66 |
< |
Cell[] as; long b, v; HashCode hc; Cell a; int n; |
66 |
> |
Cell[] as; long b, v; int[] hc; Cell a; int n; |
67 |
|
if ((as = cells) != null || |
68 |
|
!casBase(b = base, |
69 |
|
Double.doubleToRawLongBits |
70 |
|
(Double.longBitsToDouble(b) + x))) { |
71 |
|
boolean uncontended = true; |
72 |
< |
int h = (hc = threadHashCode.get()).code; |
73 |
< |
if (as == null || (n = as.length) < 1 || |
74 |
< |
(a = as[(n - 1) & h]) == null || |
72 |
> |
if ((hc = threadHashCode.get()) == null || |
73 |
> |
as == null || (n = as.length) < 1 || |
74 |
> |
(a = as[(n - 1) & hc[0]]) == null || |
75 |
|
!(uncontended = a.cas(v = a.value, |
76 |
|
Double.doubleToRawLongBits |
77 |
|
(Double.longBitsToDouble(v) + x)))) |
81 |
|
|
82 |
|
/** |
83 |
|
* Returns the current sum. The returned value is <em>NOT</em> an |
84 |
< |
* atomic snapshot: Invocation in the absence of concurrent |
84 |
> |
* atomic snapshot; invocation in the absence of concurrent |
85 |
|
* updates returns an accurate result, but concurrent updates that |
86 |
|
* occur while the sum is being calculated might not be |
87 |
< |
* incorporated. Also, because double-precision arithmetic is not |
87 |
> |
* incorporated. Also, because floating-point arithmetic is not |
88 |
|
* strictly associative, the returned result need not be identical |
89 |
|
* to the value that would be obtained in a sequential series of |
90 |
|
* updates to a single variable. |
163 |
|
|
164 |
|
/** |
165 |
|
* Returns the {@link #sum} as a {@code long} after a |
166 |
< |
* primitive conversion. |
166 |
> |
* narrowing primitive conversion. |
167 |
|
*/ |
168 |
|
public long longValue() { |
169 |
|
return (long)sum(); |
171 |
|
|
172 |
|
/** |
173 |
|
* Returns the {@link #sum} as an {@code int} after a |
174 |
< |
* primitive conversion. |
174 |
> |
* narrowing primitive conversion. |
175 |
|
*/ |
176 |
|
public int intValue() { |
177 |
|
return (int)sum(); |
179 |
|
|
180 |
|
/** |
181 |
|
* Returns the {@link #sum} as a {@code float} |
182 |
< |
* after a primitive conversion. |
182 |
> |
* after a narrowing primitive conversion. |
183 |
|
*/ |
184 |
|
public float floatValue() { |
185 |
|
return (float)sum(); |
186 |
|
} |
187 |
|
|
188 |
< |
private void writeObject(java.io.ObjectOutputStream s) |
188 |
< |
throws java.io.IOException { |
188 |
> |
private void writeObject(ObjectOutputStream s) throws IOException { |
189 |
|
s.defaultWriteObject(); |
190 |
|
s.writeDouble(sum()); |
191 |
|
} |
192 |
|
|
193 |
|
private void readObject(ObjectInputStream s) |
194 |
< |
throws IOException, ClassNotFoundException { |
194 |
> |
throws IOException, ClassNotFoundException { |
195 |
|
s.defaultReadObject(); |
196 |
|
busy = 0; |
197 |
|
cells = null; |