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; |
40 |
|
public class AtomicDouble extends Number implements java.io.Serializable { |
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 {@code AtomicDouble} with the given initial value. |
123 |
|
* if the current value is <a href="#bitEquals">bitwise equal</a> |
124 |
|
* to the expected value. |
125 |
|
* |
126 |
< |
* <p>May <a |
126 |
> |
* <p><a |
127 |
|
* href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> |
128 |
< |
* fail spuriously</a> |
129 |
< |
* and does not provide ordering guarantees, so is only rarely an |
130 |
< |
* appropriate alternative to {@code compareAndSet}. |
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 |
209 |
|
return get(); |
210 |
|
} |
211 |
|
|
212 |
+ |
/** |
213 |
+ |
* Saves the state to a stream (that is, serializes it). |
214 |
+ |
* |
215 |
+ |
* @param s the stream |
216 |
+ |
* @throws java.io.IOException if an I/O error occurs |
217 |
+ |
* @serialData The current value is emitted (a {@code double}). |
218 |
+ |
*/ |
219 |
+ |
private void writeObject(java.io.ObjectOutputStream s) |
220 |
+ |
throws java.io.IOException { |
221 |
+ |
s.defaultWriteObject(); |
222 |
+ |
|
223 |
+ |
s.writeDouble(get()); |
224 |
+ |
} |
225 |
+ |
|
226 |
+ |
/** |
227 |
+ |
* Reconstitutes the instance from a stream (that is, deserializes it). |
228 |
+ |
* @param s the stream |
229 |
+ |
* @throws ClassNotFoundException if the class of a serialized object |
230 |
+ |
* could not be found |
231 |
+ |
* @throws java.io.IOException if an I/O error occurs |
232 |
+ |
*/ |
233 |
+ |
private void readObject(java.io.ObjectInputStream s) |
234 |
+ |
throws java.io.IOException, ClassNotFoundException { |
235 |
+ |
s.defaultReadObject(); |
236 |
+ |
|
237 |
+ |
set(s.readDouble()); |
238 |
+ |
} |
239 |
+ |
|
240 |
|
// Unsafe mechanics |
241 |
|
private static final sun.misc.Unsafe unsafe = getUnsafe(); |
242 |
|
private static final long valueOffset; |
258 |
|
private static sun.misc.Unsafe getUnsafe() { |
259 |
|
try { |
260 |
|
return sun.misc.Unsafe.getUnsafe(); |
261 |
< |
} catch (SecurityException se) { |
262 |
< |
try { |
263 |
< |
return java.security.AccessController.doPrivileged |
264 |
< |
(new java.security |
265 |
< |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
266 |
< |
public sun.misc.Unsafe run() throws Exception { |
267 |
< |
java.lang.reflect.Field f = sun.misc |
268 |
< |
.Unsafe.class.getDeclaredField("theUnsafe"); |
269 |
< |
f.setAccessible(true); |
270 |
< |
return (sun.misc.Unsafe) f.get(null); |
271 |
< |
}}); |
272 |
< |
} catch (java.security.PrivilegedActionException e) { |
273 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
274 |
< |
e.getCause()); |
275 |
< |
} |
261 |
> |
} catch (SecurityException tryReflectionInstead) {} |
262 |
> |
try { |
263 |
> |
return java.security.AccessController.doPrivileged |
264 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
265 |
> |
public sun.misc.Unsafe run() throws Exception { |
266 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
267 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
268 |
> |
f.setAccessible(true); |
269 |
> |
Object x = f.get(null); |
270 |
> |
if (k.isInstance(x)) |
271 |
> |
return k.cast(x); |
272 |
> |
} |
273 |
> |
throw new NoSuchFieldError("the Unsafe"); |
274 |
> |
}}); |
275 |
> |
} catch (java.security.PrivilegedActionException e) { |
276 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
277 |
> |
e.getCause()); |
278 |
|
} |
279 |
|
} |
280 |
|
} |