java.util.concurrent.atomic.AtomicReferenceArray

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

java.util.concurrent.atomic.AtomicReferenceArray

  1. private long checkedByteOffset(int i)
  2. private static long byteOffset(int i)
  3. public AtomicReferenceArray(int length)
  4. public AtomicReferenceArray(E[] array)
  5. public final int length()
  6. public final E get(int i)
  7. private E getRaw(long offset)
  8. public final void set(int i, E newValue)
  9. public final void lazySet(int i, E newValue)
  10. public final E getAndSet(int i, E newValue)
  11. public final boolean compareAndSet(int i, E expect, E update)
  12. private boolean compareAndSetRaw(long offset, E expect, E update)
  13. public final boolean weakCompareAndSet(int i, E expect, E update)
  14. public String toString()
  15. private void readObject(java.io.ObjectInputStream s)
/*
 * %W% %E%
 *
 * Copyright (c) 2006, 2011 Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.util.concurrent.atomic;

import java.lang.reflect.Array;
import java.util.Arrays;
import sun.misc.Unsafe;

/**
 * An array of object references in which elements 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 <E> The base class of elements held in this array
 */
/**
 * 可以用原子方式更新其元素的对象引用数组。有关原子变量属性的描述,
 * 请参阅 java.util.concurrent.atomic 包规范。
 */
public class AtomicReferenceArray<E> implements java.io.Serializable {
    private static final long serialVersionUID = -6209656149925076980L;

    private static final Unsafe unsafe;
    private static final int base;
    private static final int shift;
    private static final long arrayFieldOffset;
    private final Object[] array; // must have exact type Object[]

    static {
        int scale;
        try {
            unsafe = Unsafe.getUnsafe();
            arrayFieldOffset = unsafe.objectFieldOffset
                (AtomicReferenceArray.class.getDeclaredField("array"));
            base = unsafe.arrayBaseOffset(Object[].class);
            scale = unsafe.arrayIndexScale(Object[].class);
        } catch (Exception e) {
            throw new Error(e);
        }
        if ((scale & (scale - 1)) != 0)
            throw new Error("data type scale not a power of two");
        shift = 31 - Integer.numberOfLeadingZeros(scale);
    }

    private long checkedByteOffset(int i) {
        if (i < 0 || i >= array.length)
            throw new IndexOutOfBoundsException("index " + i);

        return byteOffset(i);
    }

    private static long byteOffset(int i) {
        return ((long) i << shift) + base;
    }

    /**
     * Creates a new AtomicReferenceArray of given length.
     * @param length the length of the array
     */
    /**
     * 创建给定长度的新 AtomicReferenceArray。
     * @param  length 该数组的长度
     */
    public AtomicReferenceArray(int length) {
        array = new Object[length];
    }

    /**
     * Creates a new AtomicReferenceArray with the same length as, and
     * all elements copied from, the given array.
     *
     * @param array the array to copy elements from
     * @throws NullPointerException if array is null
     */
    /**
     * 创建与给定数组具有相同长度的新 AtomicReferenceArray,并从给定数组复制其所有元素。
     * @param  array 从中复制元素的数组
     */
    public AtomicReferenceArray(E[] array) {
        // Visibility guaranteed by final field guarantees
        this.array = Arrays.copyOf(array, array.length, Object[].class);
    }

    /**
     * Returns the length of the array.
     *
     * @return the length of the array
     */
    /**
     * 返回该数组的长度。
     * @return 数组的长度
     */
    public final int length() {
        return array.length;
    }

    /**
     * Gets the current value at position {@code i}.
     *
     * @param i the index
     * @return the current value
     */
    /**
     * 获取位置 i 的当前值。
     * @param  i 索引
     * @return   当前值
     */
    public final E get(int i) {
        return getRaw(checkedByteOffset(i));
    }

    private E getRaw(long offset) {
        return (E) unsafe.getObjectVolatile(array, offset);
    }

    /**
     * Sets the element at position {@code i} to the given value.
     *
     * @param i the index
     * @param newValue the new value
     */
    /**
     * 将位置 i 的元素设置为给定值。
     * @param i        索引
     * @param newValue 新值
     */
    public final void set(int i, E newValue) {
        unsafe.putObjectVolatile(array, checkedByteOffset(i), newValue);
    }

    /**
     * Eventually sets the element at position {@code i} to the given value.
     *
     * @param i the index
     * @param newValue the new value
     * @since 1.6
     */
    /**
     * 最终将位置 i 的元素设置为给定值。
     * @param i        索引
     * @param newValue 新值
     */
    public final void lazySet(int i, E newValue) {
        unsafe.putOrderedObject(array, checkedByteOffset(i), newValue);
    }


    /**
     * Atomically sets the element at position {@code i} to the given
     * value and returns the old value.
     *
     * @param i the index
     * @param newValue the new value
     * @return the previous value
     */
    /**
     * 以原子方式将位置 i 的元素设置为给定值,并返回旧值。
     * @param  i        索引
     * @param  newValue 新值
     * @return          以前的值
     */
    public final E getAndSet(int i, E newValue) {
        long offset = checkedByteOffset(i);
        while (true) {
            E current = getRaw(offset);
            if (compareAndSetRaw(offset, current, newValue))
                return current;
        }
    }

    /**
     * Atomically sets the element at position {@code i} to the given
     * updated value if the current value {@code ==} the expected value.
     *
     * @param i the index
     * @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.
     */
    /**
     * 如果当前值 == 预期值,则以原子方式将位置 i 的元素设置为给定的更新值。
     * @param  i      索引
     * @param  expect 预期值
     * @param  update 新值
     * @return        如果成功,则返回 true。返回 false 表示实际值与预期值不相等。
     */
    public final boolean compareAndSet(int i, E expect, E update) {
        return compareAndSetRaw(checkedByteOffset(i), expect, update);
    }

    private boolean compareAndSetRaw(long offset, E expect, E update) {
        return unsafe.compareAndSwapObject(array, offset, expect, update);
    }

    /**
     * Atomically sets the element at position {@code i} 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 i the index
     * @param expect the expected value
     * @param update the new value
     * @return true if successful.
     */
    /**
     * 如果当前值 == 预期值,则以原子方式将位置 i 的元素设置为给定的更新值。
     * 可能意外失败并且不提供排序保证,因此几乎只是 compareAndSet 的适当替换方法。
     * @param  i      索引
     * @param  expect 预期值
     * @param  update 新值
     * @return        如果成功,则返回 true
     */
    public final boolean weakCompareAndSet(int i, E expect, E update) {
        return compareAndSet(i, expect, update);
    }

    /**
     * Returns the String representation of the current values of array.
     * @return the String representation of the current values of array.
     */
    /**
     * 返回数组当前值的字符串表示形式。
     * @return 数组当前值的字符串表示形式。
     */
    public String toString() {
        int iMax = array.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(getRaw(byteOffset(i)));
            if (i == iMax)
                return b.append(']').toString();
            b.append(',').append(' ');
        }
    }

    /**
     * Reconstitutes the instance from a stream (that is, deserializes it).
     * @param s the stream
     */
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        // Note: This must be changed if any additional fields are defined
        Object a = s.readFields().get("array", null);
        if (a == null || !a.getClass().isArray())
            throw new java.io.InvalidObjectException("Not array type");
        if (a.getClass() != Object[].class)
            a = Arrays.copyOf((Object[])a, Array.getLength(a), Object[].class);
        unsafe.putObjectVolatile(this, arrayFieldOffset, a);
    }

}