java.util.concurrent.atomic.AtomicReference

jdk1.6类 java.util.concurrent.atomic.AtomicReference

java.util.concurrent.atomic.AtomicReference

  1. public AtomicReference(V initialValue)
  2. public AtomicReference()
  3. public final V get()
  4. public final void set(V newValue)
  5. public final void lazySet(V newValue)
  6. public final boolean compareAndSet(V expect, V update)
  7. public final boolean weakCompareAndSet(V expect, V update)
  8. public final V getAndSet(V newValue)
  9. public String toString()
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.util.concurrent.atomic;
import sun.misc.Unsafe;

/**
 * An object reference that may be updated atomically. See the {@link
 * java.util.concurrent.atomic} package specification for description
 * of the properties of atomic variables.
 * @since 1.5
 * @author Doug Lea
 * @param <V> The type of object referred to by this reference
 */
/**
 * 可以用原子方式更新的对象引用。有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范。
 */
public class AtomicReference<V>  implements java.io.Serializable {
    private static final long serialVersionUID = -1848883965231344442L;

    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final long valueOffset;

    static {
      try {
        valueOffset = unsafe.objectFieldOffset
            (AtomicReference.class.getDeclaredField("value"));
      } catch (Exception ex) { throw new Error(ex); }
    }

    private volatile V value;

    /**
     * Creates a new AtomicReference with the given initial value.
     *
     * @param initialValue the initial value
     */
    /**
     * 使用给定的初始值创建新的 AtomicReference。
     * @param  initialValue 初始值
     */
    public AtomicReference(V initialValue) {
        value = initialValue;
    }

    /**
     * Creates a new AtomicReference with null initial value.
     */
    /**
     * 使用 null 初始值创建新的 AtomicReference。
     */
    public AtomicReference() {
    }

    /**
     * Gets the current value.
     *
     * @return the current value
     */
    /**
     * 获取当前值。
     * @return 当前值
     */
    public final V get() {
        return value;
    }

    /**
     * Sets to the given value.
     *
     * @param newValue the new value
     */
    /**
     * 设置为给定值。
     * @param newValue 新值
     */
    public final void set(V newValue) {
        value = newValue;
    }

    /**
     * Eventually sets to the given value.
     *
     * @param newValue the new value
     * @since 1.6
     */
    /**
     * 最终设置为给定值。
     * @param newValue 新值
     */
    public final void lazySet(V newValue) {
        unsafe.putOrderedObject(this, valueOffset, newValue);
    }

    /**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     * @param expect the expected value
     * @param update the new value
     * @return true if successful. False return indicates that
     * the actual value was not equal to the expected value.
     */
    /**
     * 如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
     * @param  expect 预期值
     * @param  update 新值
     * @return        如果成功,则返回 true。返回 false 指示实际值与预期值不相等。
     */
    public final boolean compareAndSet(V expect, V update) {
        return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
    }

    /**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     *
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
     * and does not provide ordering guarantees, so is only rarely an
     * appropriate alternative to {@code compareAndSet}.
     *
     * @param expect the expected value
     * @param update the new value
     * @return true if successful.
     */
    /**
     * 如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
     * 可能意外失败并且不提供排序保证,因此几乎只是 compareAndSet 的适当替代方法。
     * @param  expect 预期值
     * @param  update 新值
     * @return        如果成功,则返回 true。
     */
    public final boolean weakCompareAndSet(V expect, V update) {
        return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
    }

    /**
     * Atomically sets to the given value and returns the old value.
     *
     * @param newValue the new value
     * @return the previous value
     */
    /**
     * 以原子方式设置为给定值,并返回旧值。
     * @param  newValue 新值
     * @return          以前的值
     */
    public final V getAndSet(V newValue) {
        while (true) {
            V x = get();
            if (compareAndSet(x, newValue))
                return x;
        }
    }

    /**
     * Returns the String representation of the current value.
     * @return the String representation of the current value.
     */
    /**
     * 返回当前值的字符串表示形式。
     * @return 当前值的字符串表示形式
     */
    public String toString() {
        return String.valueOf(get());
    }

}