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;
37 import sun.misc.Unsafe;
40 * An object reference that may be updated atomically. See the {@link
41 * java.util.concurrent.atomic} package specification for description
42 * of the properties of atomic variables.
45 * @param <V> The type of object referred to by this reference
47 public class AtomicReference<V> implements java.io.Serializable {
48 private static final long serialVersionUID = -1848883965231344442L;
50 private static final Unsafe unsafe = Unsafe.getUnsafe();
51 private static final long valueOffset;
55 valueOffset = unsafe.objectFieldOffset
56 (AtomicReference.class.getDeclaredField("value"));
57 } catch (Exception ex) { throw new Error(ex); }
60 private volatile V value;
63 * Creates a new AtomicReference with the given initial value.
65 * @param initialValue the initial value
67 public AtomicReference(V initialValue) {
72 * Creates a new AtomicReference with null initial value.
74 public AtomicReference() {
78 * Gets the current value.
80 * @return the current value
82 public final V get() {
87 * Sets to the given value.
89 * @param newValue the new value
91 public final void set(V newValue) {
96 * Eventually sets to the given value.
98 * @param newValue the new value
101 public final void lazySet(V newValue) {
102 unsafe.putOrderedObject(this, valueOffset, newValue);
106 * Atomically sets the value to the given updated value
107 * if the current value {@code ==} the expected value.
108 * @param expect the expected value
109 * @param update the new value
110 * @return true if successful. False return indicates that
111 * the actual value was not equal to the expected value.
113 public final boolean compareAndSet(V expect, V update) {
114 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
118 * Atomically sets the value to the given updated value
119 * if the current value {@code ==} the expected value.
121 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
122 * and does not provide ordering guarantees, so is only rarely an
123 * appropriate alternative to {@code compareAndSet}.
125 * @param expect the expected value
126 * @param update the new value
127 * @return true if successful.
129 public final boolean weakCompareAndSet(V expect, V update) {
130 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
134 * Atomically sets to the given value and returns the old value.
136 * @param newValue the new value
137 * @return the previous value
139 public final V getAndSet(V newValue) {
142 if (compareAndSet(x, newValue))
148 * Returns the String representation of the current value.
149 * @return the String representation of the current value.
151 public String toString() {
152 return String.valueOf(get());