2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
7 //package java.util.concurrent.atomic;
8 //import sun.misc.Unsafe;
11 * An object reference that may be updated atomically. See the {@link
12 * java.util.concurrent.atomic} package specification for description
13 * of the properties of atomic variables.
16 * @param <V> The type of object referred to by this reference
18 public class AtomicReference/*<V>*/ implements /*java.io.*/Serializable {
19 private static final long serialVersionUID = -1848883965231344442L;
21 //private static final Unsafe unsafe = Unsafe.getUnsafe();
22 // private static final long valueOffset;
26 valueOffset = unsafe.objectFieldOffset
27 (AtomicReference.class.getDeclaredField("value"));
28 } catch (Exception ex) { throw new Error(ex); }
31 private volatile Object/*V*/ value;
34 * Creates a new AtomicReference with the given initial value.
36 * @param initialValue the initial value
38 public AtomicReference(Object/*V*/ initialValue) {
43 * Creates a new AtomicReference with null initial value.
45 public AtomicReference() {
49 * Gets the current value.
51 * @return the current value
53 public final Object/*V*/ get() {
58 * Sets to the given value.
60 * @param newValue the new value
62 public final void set(Object/*V*/ newValue) {
67 * Eventually sets to the given value.
69 * @param newValue the new value
72 public final void lazySet(Object/*V*/ newValue) {
73 //unsafe.putOrderedObject(this, valueOffset, newValue);
74 System.out.println("Unimplemented AtomicReference.lazySet()!");
78 * Atomically sets the value to the given updated value
79 * if the current value <tt>==</tt> the expected value.
80 * @param expect the expected value
81 * @param update the new value
82 * @return true if successful. False return indicates that
83 * the actual value was not equal to the expected value.
85 public final boolean compareAndSet(Object/*V*/ expect, Object/*V*/ update) {
86 //return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
87 System.out.println("Unimplemented AtomicReference.compareAndSet()!");
91 * Atomically sets the value to the given updated value
92 * if the current value <tt>==</tt> the expected value.
93 * May fail spuriously and does not provide ordering guarantees,
94 * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
96 * @param expect the expected value
97 * @param update the new value
98 * @return true if successful.
100 public final boolean weakCompareAndSet(Object/*V*/ expect, Object/*V*/ update) {
101 //return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
102 System.out.println("Unimplemented AtomicReference.weakCompareAndSet()!");
106 * Atomically sets to the given value and returns the old value.
108 * @param newValue the new value
109 * @return the previous value
111 public final Object/*V*/ getAndSet(Object/*V*/ newValue) {
113 Object/*V*/ x = get();
114 if (compareAndSet(x, newValue))
120 * Returns the String representation of the current value.
121 * @return the String representation of the current value.
123 /*public String toString() {
124 return String.valueOf(get());