2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/
36 package java.util.concurrent.atomic;
39 * An object reference that may be updated atomically. See the {@link
40 * java.util.concurrent.atomic} package specification for description
41 * of the properties of atomic variables.
44 * @param <V> The type of object referred to by this reference
46 public class AtomicReference<V> implements java.io.Serializable {
47 private static final long serialVersionUID = -1848883965231344442L;
49 private volatile V value;
52 * Creates a new AtomicReference with the given initial value.
54 * @param initialValue the initial value
56 public AtomicReference(V initialValue) {
61 * Creates a new AtomicReference with null initial value.
63 public AtomicReference() {
67 * Gets the current value.
69 * @return the current value
71 public final V get() {
76 * Sets to the given value.
78 * @param newValue the new value
80 public final void set(V newValue) {
85 * Eventually sets to the given value.
87 * @param newValue the new value
90 public final void lazySet(V newValue) {
95 * Atomically sets the value to the given updated value
96 * if the current value {@code ==} the expected value.
97 * @param expect the expected value
98 * @param update the new value
99 * @return true if successful. False return indicates that
100 * the actual value was not equal to the expected value.
102 public final boolean compareAndSet(V expect, V update) {
103 if (value == expect) {
112 * Atomically sets the value to the given updated value
113 * if the current value {@code ==} the expected value.
115 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
116 * and does not provide ordering guarantees, so is only rarely an
117 * appropriate alternative to {@code compareAndSet}.
119 * @param expect the expected value
120 * @param update the new value
121 * @return true if successful.
123 public final boolean weakCompareAndSet(V expect, V update) {
124 return compareAndSet(expect, update);
128 * Atomically sets to the given value and returns the old value.
130 * @param newValue the new value
131 * @return the previous value
133 public final V getAndSet(V newValue) {
136 if (compareAndSet(x, newValue))
142 * Returns the String representation of the current value.
143 * @return the String representation of the current value.
145 public String toString() {
146 return String.valueOf(get());