java.util.concurrent.atomic.AtomicStampedReference

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

java.util.concurrent.atomic.AtomicStampedReference

  1. public AtomicStampedReference(V initialRef, int initialStamp)
  2. public V getReference()
  3. public int getStamp()
  4. public V get(int[] stampHolder)
  5. public boolean weakCompareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)
  6. public boolean compareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)
  7. public void set(V newReference, int newStamp)
  8. public boolean attemptStamp(V expectedReference, int newStamp)
/*
 * %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;

/**
 * An {@code AtomicStampedReference} maintains an object reference
 * along with an integer "stamp", that can be updated atomically.
 *
 * <p> Implementation note. This implementation maintains stamped
 * references by creating internal objects representing "boxed"
 * [reference, integer] pairs.
 *
 * @since 1.5
 * @author Doug Lea
 * @param <V> The type of object referred to by this reference
 */
/**
 * AtomicStampedReference 维护带有整数“标志”的对象引用,可以用原子方式对其进行更新。
 * 实现注意事项。通过创建表示“已装箱”的 [reference, integer] 对的内部对象,此实现维持带标志的引用。
 */
public class AtomicStampedReference<V>  {

    private static class ReferenceIntegerPair<T> {
        private final T reference;
        private final int integer;
        ReferenceIntegerPair(T r, int i) {
            reference = r; integer = i;
        }
    }

    private final AtomicReference<ReferenceIntegerPair<V>>  atomicRef;

    /**
     * Creates a new {@code AtomicStampedReference} with the given
     * initial values.
     *
     * @param initialRef the initial reference
     * @param initialStamp the initial stamp
     */
    /**
     * 创建具有给定初始值的新 AtomicStampedReference。
     * @param  initialRef   初始引用
     * @param  initialStamp 初始标志
     */
    public AtomicStampedReference(V initialRef, int initialStamp) {
        atomicRef = new AtomicReference<ReferenceIntegerPair<V>>
            (new ReferenceIntegerPair<V>(initialRef, initialStamp));
    }

    /**
     * Returns the current value of the reference.
     *
     * @return the current value of the reference
     */
    /**
     * 返回该引用的当前值。
     * @return 该引用的当前值
     */
    public V getReference() {
        return atomicRef.get().reference;
    }

    /**
     * Returns the current value of the stamp.
     *
     * @return the current value of the stamp
     */
    /**
     * 返回该标志的当前值。
     * @return 该标志的当前值
     */
    public int getStamp() {
        return atomicRef.get().integer;
    }

    /**
     * Returns the current values of both the reference and the stamp.
     * Typical usage is {@code int[1] holder; ref = v.get(holder); }.
     *
     * @param stampHolder an array of size of at least one.  On return,
     * {@code stampholder[0]} will hold the value of the stamp.
     * @return the current value of the reference
     */
    /**
     * 返回该引用和该标志的当前值。典型的用法为 int[1] holder; ref = v.get(holder); 。
     * @param  stampHolder 大小至少为 1 的数组。返回时,stampholder[0] 将保存该标志的值。
     * @return             该引用的当前值
     */
    public V get(int[] stampHolder) {
        ReferenceIntegerPair<V> p = atomicRef.get();
        stampHolder[0] = p.integer;
        return p.reference;
    }

    /**
     * Atomically sets the value of both the reference and stamp
     * to the given update values if the
     * current reference is {@code ==} to the expected reference
     * and the current stamp is equal to the expected stamp.
     *
     * <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 expectedReference the expected value of the reference
     * @param newReference the new value for the reference
     * @param expectedStamp the expected value of the stamp
     * @param newStamp the new value for the stamp
     * @return true if successful
     */
    /**
     * 如果当前引用 == 预期引用,并且当前标志等于预期标志,
     * 则以原子方式将该引用和该标志的值设置为给定的更新值。可能意外失败并且不提供排序保证,
     * 所以只有在很少的情况下才对 compareAndSet 进行适当的选择。
     * @param  expectedReference 该引用的预期值
     * @param  newReference      该引用的新值
     * @param  expectedStamp     该标志的预期值
     * @param  newStamp          该标志的新值
     * @return                   如果成功,则返回 true
     */
    public boolean weakCompareAndSet(V      expectedReference,
                                     V      newReference,
                                     int    expectedStamp,
                                     int    newStamp) {
        ReferenceIntegerPair<V> current = atomicRef.get();
        return  expectedReference == current.reference &&
            expectedStamp == current.integer &&
            ((newReference == current.reference &&
              newStamp == current.integer) ||
             atomicRef.weakCompareAndSet(current,
                                     new ReferenceIntegerPair<V>(newReference,
                                                              newStamp)));
    }

    /**
     * Atomically sets the value of both the reference and stamp
     * to the given update values if the
     * current reference is {@code ==} to the expected reference
     * and the current stamp is equal to the expected stamp.
     *
     * @param expectedReference the expected value of the reference
     * @param newReference the new value for the reference
     * @param expectedStamp the expected value of the stamp
     * @param newStamp the new value for the stamp
     * @return true if successful
     */
    /**
     * 如果当前引用 == 预期引用,并且当前标志等于预期标志,则以原子方式将该引用和该标志的值设置为给定的更新值。
     * @param  expectedReference 该引用的预期值
     * @param  newReference      该引用的新值
     * @param  expectedStamp     该标志的预期值
     * @param  newStamp          该标志的新值
     * @return                   如果成功,则返回 true
     */
    public boolean compareAndSet(V      expectedReference,
                                 V      newReference,
                                 int    expectedStamp,
                                 int    newStamp) {
        ReferenceIntegerPair<V> current = atomicRef.get();
        return  expectedReference == current.reference &&
            expectedStamp == current.integer &&
            ((newReference == current.reference &&
              newStamp == current.integer) ||
             atomicRef.compareAndSet(current,
                                     new ReferenceIntegerPair<V>(newReference,
                                                              newStamp)));
    }


    /**
     * Unconditionally sets the value of both the reference and stamp.
     *
     * @param newReference the new value for the reference
     * @param newStamp the new value for the stamp
     */
    /**
     * 无条件地同时设置该引用和标志的值。
     * @param newReference 该引用的新值
     * @param newStamp     该标志的新值
     */
    public void set(V newReference, int newStamp) {
        ReferenceIntegerPair<V> current = atomicRef.get();
        if (newReference != current.reference || newStamp != current.integer)
            atomicRef.set(new ReferenceIntegerPair<V>(newReference, newStamp));
    }

    /**
     * Atomically sets the value of the stamp to the given update value
     * if the current reference is {@code ==} to the expected
     * reference.  Any given invocation of this operation may fail
     * (return {@code false}) spuriously, but repeated invocation
     * when the current value holds the expected value and no other
     * thread is also attempting to set the value will eventually
     * succeed.
     *
     * @param expectedReference the expected value of the reference
     * @param newStamp the new value for the stamp
     * @return true if successful
     */
    /**
     * 如果当前引用 == 预期引用,则以原子方式将该标志的值设置为给定的更新值。
     * 此操作的任何给定调用都可能会意外失败(返回 false),
     * 但是在当前值保持预期值而且没有其他线程也在尝试设置该值时,重复调用将最终获得成功。
     * @param  expectedReference 该引用的预期值
     * @param  newStamp          该标志的新值
     * @return                   如果成功,则返回 true
     */
    public boolean attemptStamp(V expectedReference, int newStamp) {
        ReferenceIntegerPair<V> current = atomicRef.get();
        return  expectedReference == current.reference &&
            (newStamp == current.integer ||
             atomicRef.compareAndSet(current,
                                     new ReferenceIntegerPair<V>(expectedReference,
                                                              newStamp)));
    }
}