1 |
/* |
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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent.atomic; |
8 |
|
9 |
import sun.misc.Unsafe; |
10 |
|
11 |
/** |
12 |
* An object reference that may be updated atomically. See the {@link |
13 |
* java.util.concurrent.atomic} package specification for description |
14 |
* of the properties of atomic variables. |
15 |
* @since 1.5 |
16 |
* @author Doug Lea |
17 |
* @param <V> The type of object referred to by this reference |
18 |
*/ |
19 |
public class AtomicReference<V> implements java.io.Serializable { |
20 |
private static final long serialVersionUID = -1848883965231344442L; |
21 |
|
22 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
23 |
private static final long valueOffset; |
24 |
|
25 |
static { |
26 |
try { |
27 |
valueOffset = unsafe.objectFieldOffset |
28 |
(AtomicReference.class.getDeclaredField("value")); |
29 |
} catch (Exception ex) { throw new Error(ex); } |
30 |
} |
31 |
|
32 |
private volatile V value; |
33 |
|
34 |
/** |
35 |
* Creates a new AtomicReference with the given initial value. |
36 |
* |
37 |
* @param initialValue the initial value |
38 |
*/ |
39 |
public AtomicReference(V initialValue) { |
40 |
value = initialValue; |
41 |
} |
42 |
|
43 |
/** |
44 |
* Creates a new AtomicReference with null initial value. |
45 |
*/ |
46 |
public AtomicReference() { |
47 |
} |
48 |
|
49 |
/** |
50 |
* Gets the current value. |
51 |
* |
52 |
* @return the current value |
53 |
*/ |
54 |
public final V get() { |
55 |
return value; |
56 |
} |
57 |
|
58 |
/** |
59 |
* Sets to the given value. |
60 |
* |
61 |
* @param newValue the new value |
62 |
*/ |
63 |
public final void set(V newValue) { |
64 |
value = newValue; |
65 |
} |
66 |
|
67 |
/** |
68 |
* Eventually sets to the given value. |
69 |
* |
70 |
* @param newValue the new value |
71 |
* @since 1.6 |
72 |
*/ |
73 |
public final void lazySet(V newValue) { |
74 |
unsafe.putOrderedObject(this, valueOffset, newValue); |
75 |
} |
76 |
|
77 |
/** |
78 |
* Atomically sets the value to the given updated value |
79 |
* if the current value {@code ==} 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. |
84 |
*/ |
85 |
public final boolean compareAndSet(V expect, V update) { |
86 |
return unsafe.compareAndSwapObject(this, valueOffset, expect, update); |
87 |
} |
88 |
|
89 |
/** |
90 |
* Atomically sets the value to the given updated value |
91 |
* if the current value {@code ==} the expected value. |
92 |
* |
93 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
94 |
* spuriously and does not provide ordering guarantees</a>, so is |
95 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
96 |
* |
97 |
* @param expect the expected value |
98 |
* @param update the new value |
99 |
* @return true if successful |
100 |
*/ |
101 |
public final boolean weakCompareAndSet(V expect, V update) { |
102 |
return unsafe.compareAndSwapObject(this, valueOffset, expect, update); |
103 |
} |
104 |
|
105 |
/** |
106 |
* Atomically sets to the given value and returns the old value. |
107 |
* |
108 |
* @param newValue the new value |
109 |
* @return the previous value |
110 |
*/ |
111 |
public final V getAndSet(V newValue) { |
112 |
while (true) { |
113 |
V x = get(); |
114 |
if (compareAndSet(x, newValue)) |
115 |
return x; |
116 |
} |
117 |
} |
118 |
|
119 |
/** |
120 |
* Returns the String representation of the current value. |
121 |
* @return the String representation of the current value |
122 |
*/ |
123 |
public String toString() { |
124 |
return String.valueOf(get()); |
125 |
} |
126 |
|
127 |
} |