1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 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. 9 * 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). 15 * 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. 19 * 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 22 * questions. 23 */ 24 25 /* 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 29 * file: 30 * 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/ 34 */ 35 36 package java.util.concurrent.atomic; 37 import sun.misc.Unsafe; 38 39 /** 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. 43 * @since 1.5 44 * @author Doug Lea 45 * @param <V> The type of object referred to by this reference 46 */ 47 public class AtomicReference<V> implements java.io.Serializable { 48 private static final long serialVersionUID = -1848883965231344442L; 49 50 private static final Unsafe unsafe = Unsafe.getUnsafe(); 51 private static final long valueOffset; 52 53 static { 54 try { 55 valueOffset = unsafe.objectFieldOffset 56 (AtomicReference.class.getDeclaredField("value")); 57 } catch (Exception ex) { throw new Error(ex); } 58 } 59 60 private volatile V value; 61 62 /** 63 * Creates a new AtomicReference with the given initial value. 64 * 65 * @param initialValue the initial value 66 */ 67 public AtomicReference(V initialValue) { 68 value = initialValue; 69 } 70 71 /** 72 * Creates a new AtomicReference with null initial value. 73 */ 74 public AtomicReference() { 75 } 76 77 /** 78 * Gets the current value. 79 * 80 * @return the current value 81 */ 82 public final V get() { 83 return value; 84 } 85 86 /** 87 * Sets to the given value. 88 * 89 * @param newValue the new value 90 */ 91 public final void set(V newValue) { 92 value = newValue; 93 } 94 95 /** 96 * Eventually sets to the given value. 97 * 98 * @param newValue the new value 99 * @since 1.6 100 */ 101 public final void lazySet(V newValue) { 102 unsafe.putOrderedObject(this, valueOffset, newValue); 103 } 104 105 /** 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. 112 */ 113 public final boolean compareAndSet(V expect, V update) { 114 return unsafe.compareAndSwapObject(this, valueOffset, expect, update); 115 } 116 117 /** 118 * Atomically sets the value to the given updated value 119 * if the current value {@code ==} the expected value. 120 * 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}. 124 * 125 * @param expect the expected value 126 * @param update the new value 127 * @return true if successful 128 */ 129 public final boolean weakCompareAndSet(V expect, V update) { 130 return unsafe.compareAndSwapObject(this, valueOffset, expect, update); 131 } 132 133 /** 134 * Atomically sets to the given value and returns the old value. 135 * 136 * @param newValue the new value 137 * @return the previous value 138 */ 139 @SuppressWarnings("unchecked") 140 public final V getAndSet(V newValue) { 141 return (V)unsafe.getAndSetObject(this, valueOffset, newValue); 142 } 143 144 /** 145 * Returns the String representation of the current value. 146 * @return the String representation of the current value 147 */ 148 public String toString() { 149 return String.valueOf(get()); 150 } 151 152 }