java.util.concurrent.atomic.AtomicLongArray

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

java.util.concurrent.atomic.AtomicLongArray

  1. private long rawIndex(int i)
  2. public AtomicLongArray(int length)
  3. public AtomicLongArray(long[] array)
  4. public final int length()
  5. public final long get(int i)
  6. public final void set(int i, long newValue)
  7. public final void lazySet(int i, long newValue)
  8. public final long getAndSet(int i, long newValue)
  9. public final boolean compareAndSet(int i, long expect, long update)
  10. public final boolean weakCompareAndSet(int i, long expect, long update)
  11. public final long getAndIncrement(int i)
  12. public final long getAndDecrement(int i)
  13. public final long getAndAdd(int i, long delta)
  14. public final long incrementAndGet(int i)
  15. public final long decrementAndGet(int i)
  16. public long addAndGet(int i, long delta)
/*
 * %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;
import java.util.*;

/**
 * A {@code long} array 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
 */
/**
 * 可以用原子方式更新其元素的 long 数组。
 * 有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范。
 */
public class AtomicLongArray implements java.io.Serializable {
    private static final long serialVersionUID = -2308431214976778248L;

    // setup to use Unsafe.compareAndSwapInt for updates
    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final int base = unsafe.arrayBaseOffset(long[].class);
    private static final int scale = unsafe.arrayIndexScale(long[].class);
    private final long[] array;

    private long rawIndex(int i) {
        if (i < 0 || i >= array.length)
            throw new IndexOutOfBoundsException("index " + i);
        return base + (long) i * scale;
    }

    /**
     * Creates a new AtomicLongArray of given length.
     *
     * @param length the length of the array
     */
    /**
     * 创建给定长度的新 AtomicLongArray。
     * @param  length 该数组的长度
     */
    public AtomicLongArray(int length) {
        array = new long[length];
        // must perform at least one volatile write to conform to JMM
        if (length > 0)
            unsafe.putLongVolatile(array, rawIndex(0), 0);
    }

    /**
     * Creates a new AtomicLongArray 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
     */
    /**
     * 创建与给定数组具有相同长度的新 AtomicLongArray,并从给定数组复制其所有元素。
     * @param  array 从中复制元素的数组
     */
    public AtomicLongArray(long[] array) {
        if (array == null)
            throw new NullPointerException();
        int length = array.length;
        this.array = new long[length];
        if (length > 0) {
            int last = length-1;
            for (int i = 0; i < last; ++i)
                this.array[i] = array[i];
            // Do the last write as volatile
            unsafe.putLongVolatile(this.array, rawIndex(last), array[last]);
        }
    }

    /**
     * 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 long get(int i) {
        return unsafe.getLongVolatile(array, rawIndex(i));
    }

    /**
     * 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, long newValue) {
        unsafe.putLongVolatile(array, rawIndex(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, long newValue) {
        unsafe.putOrderedLong(array, rawIndex(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 long getAndSet(int i, long newValue) {
        while (true) {
            long current = get(i);
            if (compareAndSet(i, current, newValue))
                return current;
        }
    }

    /**
     * Atomically sets the value 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.
     */
    /**
     * 如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
     * @param  i      索引
     * @param  expect 预期值
     * @param  update 新值
     * @return        如果成功,则返回 true。返回 false 指示实际值与预期值不相等。
     */
    public final boolean compareAndSet(int i, long expect, long update) {
        return unsafe.compareAndSwapLong(array, rawIndex(i),
                                         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 i the index
     * @param expect the expected value
     * @param update the new value
     * @return true if successful.
     */
    /**
     * 如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
     * 可能意外失败并且不提供排序保证,所以只能在很少的情况下对 compareAndSet 进行适当地选择。
     * @param  i      索引
     * @param  expect 预期值
     * @param  update 新值
     * @return        如果成功,则返回 true。
     */
    public final boolean weakCompareAndSet(int i, long expect, long update) {
        return compareAndSet(i, expect, update);
    }

    /**
     * Atomically increments by one the element at index {@code i}.
     *
     * @param i the index
     * @return the previous value
     */
    /**
     * 以原子方式将索引 i 的元素加 1。
     * @param  i 索引
     * @return   以前的值
     */
    public final long getAndIncrement(int i) {
        while (true) {
            long current = get(i);
            long next = current + 1;
            if (compareAndSet(i, current, next))
                return current;
        }
    }

    /**
     * Atomically decrements by one the element at index {@code i}.
     *
     * @param i the index
     * @return the previous value
     */
    /**
     * 以原子方式将索引 i 的元素减 1。
     * @param  i 索引
     * @return   以前的值
     */
    public final long getAndDecrement(int i) {
        while (true) {
            long current = get(i);
            long next = current - 1;
            if (compareAndSet(i, current, next))
                return current;
        }
    }

    /**
     * Atomically adds the given value to the element at index {@code i}.
     *
     * @param i the index
     * @param delta the value to add
     * @return the previous value
     */
    /**
     * 以原子方式将给定值与索引 i 的元素相加。
     * @param  i     索引
     * @param  delta 要加上的值
     * @return       以前的值
     */
    public final long getAndAdd(int i, long delta) {
        while (true) {
            long current = get(i);
            long next = current + delta;
            if (compareAndSet(i, current, next))
                return current;
        }
    }

    /**
     * Atomically increments by one the element at index {@code i}.
     *
     * @param i the index
     * @return the updated value
     */
    /**
     * 以原子方式将索引 i 的元素加1。
     * @param  i 索引
     * @return   更新的值
     */
    public final long incrementAndGet(int i) {
        while (true) {
            long current = get(i);
            long next = current + 1;
            if (compareAndSet(i, current, next))
                return next;
        }
    }

    /**
     * Atomically decrements by one the element at index {@code i}.
     *
     * @param i the index
     * @return the updated value
     */
    /**
     * 以原子方式将索引 i 的元素减1。
     * @param  i  索引
     * @return    更新的值
     */
    public final long decrementAndGet(int i) {
        while (true) {
            long current = get(i);
            long next = current - 1;
            if (compareAndSet(i, current, next))
                return next;
        }
    }

    /**
     * Atomically adds the given value to the element at index {@code i}.
     *
     * @param i the index
     * @param delta the value to add
     * @return the updated value
     */
    /**
     * 以原子方式将给定值添加到索引 i 的元素。
     * @param  i     索引
     * @param  delta 要添加的值
     * @return       更新的值
     */
    public long addAndGet(int i, long delta) {
        while (true) {
            long current = get(i);
            long next = current + delta;
            if (compareAndSet(i, current, next))
                return next;
        }
    }

    /**
     * Returns the String representation of the current values of array.
     * @return the String representation of the current values of array.
     */
    /**
     * 返回数组当前值的字符串表示形式。
     * @return 数组当前值的字符串表示形式。
     */
    public String toString() {
        if (array.length > 0) // force volatile read
            get(0);
        return Arrays.toString(array);
    }

}