jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1334
|
3 |
*
|
jtulach@1334
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1334
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1334
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1334
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1334
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1334
|
9 |
*
|
jtulach@1334
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1334
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1334
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1334
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1334
|
14 |
* accompanied this code).
|
jtulach@1334
|
15 |
*
|
jtulach@1334
|
16 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1334
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1334
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1334
|
19 |
*
|
jtulach@1334
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1334
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1334
|
22 |
* questions.
|
jtulach@1334
|
23 |
*/
|
jtulach@1334
|
24 |
|
jtulach@1334
|
25 |
/*
|
jtulach@1334
|
26 |
* This file is available under and governed by the GNU General Public
|
jtulach@1334
|
27 |
* License version 2 only, as published by the Free Software Foundation.
|
jtulach@1334
|
28 |
* However, the following notice accompanied the original version of this
|
jtulach@1334
|
29 |
* file:
|
jtulach@1334
|
30 |
*
|
jtulach@1334
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
jtulach@1334
|
32 |
* Expert Group and released to the public domain, as explained at
|
jtulach@1334
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
jtulach@1334
|
34 |
*/
|
jtulach@1334
|
35 |
|
jtulach@1334
|
36 |
package java.util.concurrent.atomic;
|
jtulach@1334
|
37 |
|
jtulach@1334
|
38 |
/**
|
jtulach@1334
|
39 |
* An object reference that may be updated atomically. See the {@link
|
jtulach@1334
|
40 |
* java.util.concurrent.atomic} package specification for description
|
jtulach@1334
|
41 |
* of the properties of atomic variables.
|
jtulach@1334
|
42 |
* @since 1.5
|
jtulach@1334
|
43 |
* @author Doug Lea
|
jtulach@1334
|
44 |
* @param <V> The type of object referred to by this reference
|
jtulach@1334
|
45 |
*/
|
jtulach@1334
|
46 |
public class AtomicReference<V> implements java.io.Serializable {
|
jtulach@1334
|
47 |
private static final long serialVersionUID = -1848883965231344442L;
|
jtulach@1334
|
48 |
|
jtulach@1334
|
49 |
private volatile V value;
|
jtulach@1334
|
50 |
|
jtulach@1334
|
51 |
/**
|
jtulach@1334
|
52 |
* Creates a new AtomicReference with the given initial value.
|
jtulach@1334
|
53 |
*
|
jtulach@1334
|
54 |
* @param initialValue the initial value
|
jtulach@1334
|
55 |
*/
|
jtulach@1334
|
56 |
public AtomicReference(V initialValue) {
|
jtulach@1334
|
57 |
value = initialValue;
|
jtulach@1334
|
58 |
}
|
jtulach@1334
|
59 |
|
jtulach@1334
|
60 |
/**
|
jtulach@1334
|
61 |
* Creates a new AtomicReference with null initial value.
|
jtulach@1334
|
62 |
*/
|
jtulach@1334
|
63 |
public AtomicReference() {
|
jtulach@1334
|
64 |
}
|
jtulach@1334
|
65 |
|
jtulach@1334
|
66 |
/**
|
jtulach@1334
|
67 |
* Gets the current value.
|
jtulach@1334
|
68 |
*
|
jtulach@1334
|
69 |
* @return the current value
|
jtulach@1334
|
70 |
*/
|
jtulach@1334
|
71 |
public final V get() {
|
jtulach@1334
|
72 |
return value;
|
jtulach@1334
|
73 |
}
|
jtulach@1334
|
74 |
|
jtulach@1334
|
75 |
/**
|
jtulach@1334
|
76 |
* Sets to the given value.
|
jtulach@1334
|
77 |
*
|
jtulach@1334
|
78 |
* @param newValue the new value
|
jtulach@1334
|
79 |
*/
|
jtulach@1334
|
80 |
public final void set(V newValue) {
|
jtulach@1334
|
81 |
value = newValue;
|
jtulach@1334
|
82 |
}
|
jtulach@1334
|
83 |
|
jtulach@1334
|
84 |
/**
|
jtulach@1334
|
85 |
* Eventually sets to the given value.
|
jtulach@1334
|
86 |
*
|
jtulach@1334
|
87 |
* @param newValue the new value
|
jtulach@1334
|
88 |
* @since 1.6
|
jtulach@1334
|
89 |
*/
|
jtulach@1334
|
90 |
public final void lazySet(V newValue) {
|
jaroslav@1338
|
91 |
value = newValue;
|
jtulach@1334
|
92 |
}
|
jtulach@1334
|
93 |
|
jtulach@1334
|
94 |
/**
|
jtulach@1334
|
95 |
* Atomically sets the value to the given updated value
|
jtulach@1334
|
96 |
* if the current value {@code ==} the expected value.
|
jtulach@1334
|
97 |
* @param expect the expected value
|
jtulach@1334
|
98 |
* @param update the new value
|
jtulach@1334
|
99 |
* @return true if successful. False return indicates that
|
jtulach@1334
|
100 |
* the actual value was not equal to the expected value.
|
jtulach@1334
|
101 |
*/
|
jtulach@1334
|
102 |
public final boolean compareAndSet(V expect, V update) {
|
jaroslav@1338
|
103 |
if (value == expect) {
|
jaroslav@1338
|
104 |
value = update;
|
jaroslav@1338
|
105 |
return true;
|
jaroslav@1338
|
106 |
} else {
|
jaroslav@1338
|
107 |
return false;
|
jaroslav@1338
|
108 |
}
|
jtulach@1334
|
109 |
}
|
jtulach@1334
|
110 |
|
jtulach@1334
|
111 |
/**
|
jtulach@1334
|
112 |
* Atomically sets the value to the given updated value
|
jtulach@1334
|
113 |
* if the current value {@code ==} the expected value.
|
jtulach@1334
|
114 |
*
|
jtulach@1334
|
115 |
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
|
jtulach@1334
|
116 |
* and does not provide ordering guarantees, so is only rarely an
|
jtulach@1334
|
117 |
* appropriate alternative to {@code compareAndSet}.
|
jtulach@1334
|
118 |
*
|
jtulach@1334
|
119 |
* @param expect the expected value
|
jtulach@1334
|
120 |
* @param update the new value
|
jtulach@1334
|
121 |
* @return true if successful.
|
jtulach@1334
|
122 |
*/
|
jtulach@1334
|
123 |
public final boolean weakCompareAndSet(V expect, V update) {
|
jaroslav@1338
|
124 |
return compareAndSet(expect, update);
|
jtulach@1334
|
125 |
}
|
jtulach@1334
|
126 |
|
jtulach@1334
|
127 |
/**
|
jtulach@1334
|
128 |
* Atomically sets to the given value and returns the old value.
|
jtulach@1334
|
129 |
*
|
jtulach@1334
|
130 |
* @param newValue the new value
|
jtulach@1334
|
131 |
* @return the previous value
|
jtulach@1334
|
132 |
*/
|
jtulach@1334
|
133 |
public final V getAndSet(V newValue) {
|
jtulach@1334
|
134 |
while (true) {
|
jtulach@1334
|
135 |
V x = get();
|
jtulach@1334
|
136 |
if (compareAndSet(x, newValue))
|
jtulach@1334
|
137 |
return x;
|
jtulach@1334
|
138 |
}
|
jtulach@1334
|
139 |
}
|
jtulach@1334
|
140 |
|
jtulach@1334
|
141 |
/**
|
jtulach@1334
|
142 |
* Returns the String representation of the current value.
|
jtulach@1334
|
143 |
* @return the String representation of the current value.
|
jtulach@1334
|
144 |
*/
|
jtulach@1334
|
145 |
public String toString() {
|
jtulach@1334
|
146 |
return String.valueOf(get());
|
jtulach@1334
|
147 |
}
|
jtulach@1334
|
148 |
|
jtulach@1334
|
149 |
}
|