19 |
|
* this class does extend {@code Number} to allow uniform access by |
20 |
|
* tools and utilities that deal with numerically-based classes. |
21 |
|
* |
22 |
< |
* <p><a name="bitEquals">This class compares primitive {@code double} |
22 |
> |
* <p id="bitEquals">This class compares primitive {@code double} |
23 |
|
* values in methods such as {@link #compareAndSet} by comparing their |
24 |
|
* bitwise representation using {@link Double#doubleToRawLongBits}, |
25 |
|
* which differs from both the primitive double {@code ==} operator |
26 |
|
* and from {@link Double#equals}, as if implemented by: |
27 |
|
* <pre> {@code |
28 |
< |
* boolean bitEquals(double x, double y) { |
28 |
> |
* static boolean bitEquals(double x, double y) { |
29 |
|
* long xBits = Double.doubleToRawLongBits(x); |
30 |
|
* long yBits = Double.doubleToRawLongBits(y); |
31 |
|
* return xBits == yBits; |
32 |
|
* }}</pre> |
33 |
|
* |
34 |
+ |
* @see jsr166e.DoubleAdder |
35 |
+ |
* @see jsr166e.DoubleMaxUpdater |
36 |
+ |
* |
37 |
|
* @author Doug Lea |
38 |
|
* @author Martin Buchholz |
39 |
|
*/ |
40 |
|
public class AtomicDouble extends Number implements java.io.Serializable { |
41 |
< |
static final long serialVersionUID = -8405198993435143622L; |
41 |
> |
private static final long serialVersionUID = -8405198993435143622L; |
42 |
|
|
43 |
< |
private volatile long value; |
43 |
> |
private transient volatile long value; |
44 |
|
|
45 |
|
/** |
46 |
< |
* Creates a new AtomicDouble with the given initial value. |
46 |
> |
* Creates a new {@code AtomicDouble} with the given initial value. |
47 |
|
* |
48 |
|
* @param initialValue the initial value |
49 |
|
*/ |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
< |
* Creates a new AtomicDouble with initial value {@code 0.0}. |
55 |
> |
* Creates a new {@code AtomicDouble} with initial value {@code 0.0}. |
56 |
|
*/ |
57 |
< |
public AtomicDouble() { this(0.0); } |
57 |
> |
public AtomicDouble() { |
58 |
> |
// assert doubleToRawLongBits(0.0) == 0L; |
59 |
> |
} |
60 |
|
|
61 |
|
/** |
62 |
|
* Gets the current value. |
123 |
|
* if the current value is <a href="#bitEquals">bitwise equal</a> |
124 |
|
* to the expected value. |
125 |
|
* |
126 |
< |
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a> |
127 |
< |
* and does not provide ordering guarantees, so is only rarely an |
128 |
< |
* appropriate alternative to {@code compareAndSet}. |
126 |
> |
* <p><a |
127 |
> |
* href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> |
128 |
> |
* May fail spuriously and does not provide ordering guarantees</a>, |
129 |
> |
* so is only rarely an appropriate alternative to {@code compareAndSet}. |
130 |
|
* |
131 |
|
* @param expect the expected value |
132 |
|
* @param update the new value |
183 |
|
* after a narrowing primitive conversion. |
184 |
|
*/ |
185 |
|
public int intValue() { |
186 |
< |
return (int)get(); |
186 |
> |
return (int) get(); |
187 |
|
} |
188 |
|
|
189 |
|
/** |
191 |
|
* after a narrowing primitive conversion. |
192 |
|
*/ |
193 |
|
public long longValue() { |
194 |
< |
return (long)get(); |
194 |
> |
return (long) get(); |
195 |
|
} |
196 |
|
|
197 |
|
/** |
199 |
|
* after a narrowing primitive conversion. |
200 |
|
*/ |
201 |
|
public float floatValue() { |
202 |
< |
return (float)get(); |
202 |
> |
return (float) get(); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
209 |
|
return get(); |
210 |
|
} |
211 |
|
|
212 |
+ |
/** |
213 |
+ |
* Saves the state to a stream (that is, serializes it). |
214 |
+ |
* |
215 |
+ |
* @serialData The current value is emitted (a {@code double}). |
216 |
+ |
*/ |
217 |
+ |
private void writeObject(java.io.ObjectOutputStream s) |
218 |
+ |
throws java.io.IOException { |
219 |
+ |
s.defaultWriteObject(); |
220 |
+ |
|
221 |
+ |
s.writeDouble(get()); |
222 |
+ |
} |
223 |
+ |
|
224 |
+ |
/** |
225 |
+ |
* Reconstitutes the instance from a stream (that is, deserializes it). |
226 |
+ |
*/ |
227 |
+ |
private void readObject(java.io.ObjectInputStream s) |
228 |
+ |
throws java.io.IOException, ClassNotFoundException { |
229 |
+ |
s.defaultReadObject(); |
230 |
+ |
|
231 |
+ |
set(s.readDouble()); |
232 |
+ |
} |
233 |
+ |
|
234 |
|
// Unsafe mechanics |
235 |
|
private static final sun.misc.Unsafe unsafe = getUnsafe(); |
236 |
|
private static final long valueOffset; |
252 |
|
private static sun.misc.Unsafe getUnsafe() { |
253 |
|
try { |
254 |
|
return sun.misc.Unsafe.getUnsafe(); |
255 |
< |
} catch (SecurityException se) { |
256 |
< |
try { |
257 |
< |
return java.security.AccessController.doPrivileged |
258 |
< |
(new java.security |
259 |
< |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
260 |
< |
public sun.misc.Unsafe run() throws Exception { |
261 |
< |
java.lang.reflect.Field f = sun.misc |
262 |
< |
.Unsafe.class.getDeclaredField("theUnsafe"); |
263 |
< |
f.setAccessible(true); |
264 |
< |
return (sun.misc.Unsafe) f.get(null); |
265 |
< |
}}); |
266 |
< |
} catch (java.security.PrivilegedActionException e) { |
267 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
268 |
< |
e.getCause()); |
269 |
< |
} |
255 |
> |
} catch (SecurityException tryReflectionInstead) {} |
256 |
> |
try { |
257 |
> |
return java.security.AccessController.doPrivileged |
258 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
259 |
> |
public sun.misc.Unsafe run() throws Exception { |
260 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
261 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
262 |
> |
f.setAccessible(true); |
263 |
> |
Object x = f.get(null); |
264 |
> |
if (k.isInstance(x)) |
265 |
> |
return k.cast(x); |
266 |
> |
} |
267 |
> |
throw new NoSuchFieldError("the Unsafe"); |
268 |
> |
}}); |
269 |
> |
} catch (java.security.PrivilegedActionException e) { |
270 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
271 |
> |
e.getCause()); |
272 |
|
} |
273 |
|
} |
244 |
– |
|
274 |
|
} |