java.util.concurrent.atomic.AtomicMarkableReference

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

java.util.concurrent.atomic.AtomicMarkableReference

  1. private static class ReferenceBooleanPair<T>
  2. public AtomicMarkableReference(V initialRef, boolean initialMark)
  3. public V getReference()
  4. public boolean isMarked()
  5. public V get(boolean[] markHolder)
  6. public boolean weakCompareAndSet(V expectedReference, V newReference, boolean expectedMark, boolean newMark)
  7. public boolean compareAndSet(V expectedReference, V newReference, boolean expectedMark, boolean newMark)
  8. public void set(V newReference, boolean newMark)
  9. public boolean attemptMark(V expectedReference, boolean newMark)

/*
 * %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 AtomicMarkableReference} maintains an object reference
 * along with a mark bit, that can be updated atomically.
 * <p>
 * <p> Implementation note. This implementation maintains markable
 * references by creating internal objects representing "boxed"
 * [reference, boolean] pairs.
 *
 * @since 1.5
 * @author Doug Lea
 * @param <V> The type of object referred to by this reference
 */
/**
 * AtomicMarkableReference 维护带有标记位的对象引用,可以原子方式对其进行更新。
 * 实现注意事项。通过创建表示“已装箱”的 [reference, boolean] 对的内部对象,此实现维持可标记的引用。
 */
public class AtomicMarkableReference<V>  {

    private static class ReferenceBooleanPair<T> {
        private final T reference;
        private final boolean bit;
        ReferenceBooleanPair(T r, boolean i) {
            reference = r; bit = i;
        }
    }

    private final AtomicReference<ReferenceBooleanPair<V>>  atomicRef;

    /**
     * Creates a new {@code AtomicMarkableReference} with the given
     * initial values.
     *
     * @param initialRef the initial reference
     * @param initialMark the initial mark
     */
    /**
     * 创建具有给定初始值的新 AtomicMarkableReference。
     * @param  initialRef  初始引用
     * @param  initialMark 初始标记
     */
    public AtomicMarkableReference(V initialRef, boolean initialMark) {
        atomicRef = new AtomicReference<ReferenceBooleanPair<V>> (new ReferenceBooleanPair<V>(initialRef, initialMark));
    }

    /**
     * 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 mark.
     *
     * @return the current value of the mark
     */
    /**
     * 返回该标记的当前值。
     * @return 该标记的当前值
     */
    public boolean isMarked() {
        return atomicRef.get().bit;
    }

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

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

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

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

    /**
     * Atomically sets the value of the mark 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 newMark the new value for the mark
     * @return true if successful
     */
    /**
     * 如果当前引用 == 预期引用,则以原子方式将该标记的值设置为给定的更新值。
     * 此操作的任何给定调用都可能会意外失败(返回 false),
     * 但是在当前值保持预期值而且没有其他线程也在尝试设置该值时,重复调用将最终获得成功。
     * @param  expectedReference 该引用的预期值
     * @param  newMark           该标记的新值
     * @return                   如果成功,则返回 true
     */
    public boolean attemptMark(V expectedReference, boolean newMark) {
        ReferenceBooleanPair<V> current = atomicRef.get();
        return  expectedReference == current.reference &&
            (newMark == current.bit ||
             atomicRef.compareAndSet
             (current, new ReferenceBooleanPair<V>(expectedReference,
                                                newMark)));
    }
}