5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166e; |
8 |
– |
import java.io.IOException; |
8 |
|
import java.io.Serializable; |
10 |
– |
import java.io.ObjectInputStream; |
11 |
– |
import java.io.ObjectOutputStream; |
9 |
|
|
10 |
|
/** |
11 |
|
* One or more variables that together maintain an initially zero |
16 |
|
* variables maintaining the sum. |
17 |
|
* |
18 |
|
* <p>This class extends {@link Number}, but does <em>not</em> define |
19 |
< |
* methods such as {@code hashCode} and {@code compareTo} because |
20 |
< |
* instances are expected to be mutated, and so are not useful as |
21 |
< |
* collection keys. |
19 |
> |
* methods such as {@code equals}, {@code hashCode} and {@code |
20 |
> |
* compareTo} because instances are expected to be mutated, and so are |
21 |
> |
* not useful as collection keys. |
22 |
|
* |
23 |
|
* <p><em>jsr166e note: This class is targeted to be placed in |
24 |
< |
* java.util.concurrent.atomic<em> |
24 |
> |
* java.util.concurrent.atomic.</em> |
25 |
|
* |
26 |
|
* @since 1.8 |
27 |
|
* @author Doug Lea |
59 |
|
* @param x the value to add |
60 |
|
*/ |
61 |
|
public void add(double 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 || |
64 |
|
!casBase(b = base, |
65 |
|
Double.doubleToRawLongBits |
66 |
|
(Double.longBitsToDouble(b) + x))) { |
67 |
|
boolean uncontended = true; |
68 |
< |
int h = (hc = threadHashCode.get()).code; |
69 |
< |
if (as == null || (n = as.length) < 1 || |
70 |
< |
(a = as[(n - 1) & h]) == null || |
68 |
> |
if ((hc = threadHashCode.get()) == null || |
69 |
> |
as == null || (n = as.length) < 1 || |
70 |
> |
(a = as[(n - 1) & hc[0]]) == null || |
71 |
|
!(uncontended = a.cas(v = a.value, |
72 |
|
Double.doubleToRawLongBits |
73 |
|
(Double.longBitsToDouble(v) + x)))) |
77 |
|
|
78 |
|
/** |
79 |
|
* Returns the current sum. The returned value is <em>NOT</em> an |
80 |
< |
* atomic snapshot: Invocation in the absence of concurrent |
80 |
> |
* atomic snapshot; invocation in the absence of concurrent |
81 |
|
* updates returns an accurate result, but concurrent updates that |
82 |
|
* occur while the sum is being calculated might not be |
83 |
< |
* incorporated. Also, because double-precision arithmetic is not |
83 |
> |
* incorporated. Also, because floating-point arithmetic is not |
84 |
|
* strictly associative, the returned result need not be identical |
85 |
|
* to the value that would be obtained in a sequential series of |
86 |
|
* updates to a single variable. |
142 |
|
|
143 |
|
/** |
144 |
|
* Returns the String representation of the {@link #sum}. |
145 |
< |
* @return the String representation of the {@link #sum}. |
145 |
> |
* @return the String representation of the {@link #sum} |
146 |
|
*/ |
147 |
|
public String toString() { |
148 |
|
return Double.toString(sum()); |
159 |
|
|
160 |
|
/** |
161 |
|
* Returns the {@link #sum} as a {@code long} after a |
162 |
< |
* primitive conversion. |
162 |
> |
* narrowing primitive conversion. |
163 |
|
*/ |
164 |
|
public long longValue() { |
165 |
|
return (long)sum(); |
167 |
|
|
168 |
|
/** |
169 |
|
* Returns the {@link #sum} as an {@code int} after a |
170 |
< |
* primitive conversion. |
170 |
> |
* narrowing primitive conversion. |
171 |
|
*/ |
172 |
|
public int intValue() { |
173 |
|
return (int)sum(); |
175 |
|
|
176 |
|
/** |
177 |
|
* Returns the {@link #sum} as a {@code float} |
178 |
< |
* after a primitive conversion. |
178 |
> |
* after a narrowing primitive conversion. |
179 |
|
*/ |
180 |
|
public float floatValue() { |
181 |
|
return (float)sum(); |
187 |
|
s.writeDouble(sum()); |
188 |
|
} |
189 |
|
|
190 |
< |
private void readObject(ObjectInputStream s) |
191 |
< |
throws IOException, ClassNotFoundException { |
190 |
> |
private void readObject(java.io.ObjectInputStream s) |
191 |
> |
throws java.io.IOException, ClassNotFoundException { |
192 |
|
s.defaultReadObject(); |
193 |
|
busy = 0; |
194 |
|
cells = null; |