0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/Apackage java.util.concurrent.atomic;
0N/Aimport sun.misc.Unsafe;
0N/A
0N/A/**
0N/A * An object reference that may be updated atomically. See the {@link
0N/A * java.util.concurrent.atomic} package specification for description
0N/A * of the properties of atomic variables.
0N/A * @since 1.5
0N/A * @author Doug Lea
0N/A * @param <V> The type of object referred to by this reference
0N/A */
0N/Apublic class AtomicReference<V> implements java.io.Serializable {
0N/A private static final long serialVersionUID = -1848883965231344442L;
0N/A
0N/A private static final Unsafe unsafe = Unsafe.getUnsafe();
0N/A private static final long valueOffset;
0N/A
0N/A static {
0N/A try {
0N/A valueOffset = unsafe.objectFieldOffset
0N/A (AtomicReference.class.getDeclaredField("value"));
0N/A } catch (Exception ex) { throw new Error(ex); }
0N/A }
0N/A
0N/A private volatile V value;
0N/A
0N/A /**
0N/A * Creates a new AtomicReference with the given initial value.
0N/A *
0N/A * @param initialValue the initial value
0N/A */
0N/A public AtomicReference(V initialValue) {
0N/A value = initialValue;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new AtomicReference with null initial value.
0N/A */
0N/A public AtomicReference() {
0N/A }
0N/A
0N/A /**
0N/A * Gets the current value.
0N/A *
0N/A * @return the current value
0N/A */
0N/A public final V get() {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Sets to the given value.
0N/A *
0N/A * @param newValue the new value
0N/A */
0N/A public final void set(V newValue) {
0N/A value = newValue;
0N/A }
0N/A
0N/A /**
0N/A * Eventually sets to the given value.
0N/A *
0N/A * @param newValue the new value
0N/A * @since 1.6
0N/A */
0N/A public final void lazySet(V newValue) {
0N/A unsafe.putOrderedObject(this, valueOffset, newValue);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the value to the given updated value
0N/A * if the current value {@code ==} the expected value.
0N/A * @param expect the expected value
0N/A * @param update the new value
0N/A * @return true if successful. False return indicates that
0N/A * the actual value was not equal to the expected value.
0N/A */
0N/A public final boolean compareAndSet(V expect, V update) {
0N/A return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the value to the given updated value
0N/A * if the current value {@code ==} the expected value.
0N/A *
0N/A * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
0N/A * and does not provide ordering guarantees, so is only rarely an
0N/A * appropriate alternative to {@code compareAndSet}.
0N/A *
0N/A * @param expect the expected value
0N/A * @param update the new value
0N/A * @return true if successful.
0N/A */
0N/A public final boolean weakCompareAndSet(V expect, V update) {
0N/A return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets to the given value and returns the old value.
0N/A *
0N/A * @param newValue the new value
0N/A * @return the previous value
0N/A */
0N/A public final V getAndSet(V newValue) {
0N/A while (true) {
0N/A V x = get();
0N/A if (compareAndSet(x, newValue))
0N/A return x;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the String representation of the current value.
0N/A * @return the String representation of the current value.
0N/A */
0N/A public String toString() {
0N/A return String.valueOf(get());
0N/A }
0N/A
0N/A}