java.util.concurrent.atomic.AtomicInteger

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

转自类 AtomicInteger

java.util.concurrent.atomic.AtomicInteger

  1. public AtomicInteger(int initialValue)
  2. public AtomicInteger()
  3. public final int get()
  4. public final void set(int newValue)
  5. public final void lazySet(int newValue)
  6. public final int getAndSet(int newValue)
  7. public final boolean compareAndSet(int expect, int update)
  8. public final boolean weakCompareAndSet(int expect, int update)
  9. public final int getAndIncrement()
  10. public final int getAndDecrement()
  11. public final int getAndAdd(int delta)
  12. public final int incrementAndGet()
  13. public final int decrementAndGet()
  14. public final int addAndGet(int delta)
  15. public String toString()
  16. public int intValue()
  17. public long longValue()
  18. public float floatValue()
  19. public double doubleValue()
/*
 * %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 {@code int} value that may be updated atomically.  See the
 * {@link java.util.concurrent.atomic} package specification for
 * description of the properties of atomic variables. An
 * {@code AtomicInteger} is used in applications such as atomically
 * incremented counters, and cannot be used as a replacement for an
 * {@link java.lang.Integer}. However, this class does extend
 * {@code Number} to allow uniform access by tools and utilities that
 * deal with numerically-based classes.
 *
 * @since 1.5
 * @author Doug Lea
*/
/**
 * 可以用原子方式更新的 int 值。有关原子变量属性的描述,
 * 请参阅 java.util.concurrent.atomic 包规范。
 * AtomicInteger 可用在应用程序中(如以原子方式增加的计数器),并且不能用于
 * 替换 Integer。但是,此类确实扩展了 Number,允许那些处理基于数字类的工具和
 * 实用工具进行统一访问。
 */
public class AtomicInteger extends Number implements java.io.Serializable {
    private static final long serialVersionUID = 6214790243416807050L;

    // setup to use Unsafe.compareAndSwapInt for updates
    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final long valueOffset;

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

    private volatile int value;

    /**
     * Creates a new AtomicInteger with the given initial value.
     *
     * @param initialValue the initial value
     */
    /**
     * 创建具有给定初始值的新 AtomicInteger。
     * @param  initialValue 初始值
     */
    public AtomicInteger(int initialValue) {
        value = initialValue;
    }

    /**
     * Creates a new AtomicInteger with initial value {@code 0}.
     */
    /**
     * 创建具有初始值 0 的新 AtomicInteger。
     */
    public AtomicInteger() {
    }

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

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

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

    /**
     * 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 int getAndSet(int newValue) {
        for (;;) {
            int current = get();
            if (compareAndSet(current, newValue))
                return current;
        }
    }

    /**
     * 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(int expect, int update) {
    return unsafe.compareAndSwapInt(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(int expect, int update) {
    return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }

    /**
     * Atomically increments by one the current value.
     *
     * @return the previous value
     */
    /**
     * 以原子方式将当前值加 1。
     * @return 以前的值
     */
    public final int getAndIncrement() {
        for (;;) {
            int current = get();
            int next = current + 1;
            if (compareAndSet(current, next))
                return current;
        }
    }

    /**
     * Atomically decrements by one the current value.
     *
     * @return the previous value
     */
    /**
     * 以原子方式将当前值减 1。
     * @return 以前的值
     */
    public final int getAndDecrement() {
        for (;;) {
            int current = get();
            int next = current - 1;
            if (compareAndSet(current, next))
                return current;
        }
    }

    /**
     * Atomically adds the given value to the current value.
     *
     * @param delta the value to add
     * @return the previous value
     */
    /**
     * 以原子方式将给定值与当前值相加。
     * @param  delta 要加上的值
     * @return       以前的值
     */
    public final int getAndAdd(int delta) {
        for (;;) {
            int current = get();
            int next = current + delta;
            if (compareAndSet(current, next))
                return current;
        }
    }

    /**
     * Atomically increments by one the current value.
     *
     * @return the updated value
     */
    /**
     * 以原子方式将当前值加 1。
     * @return 更新的值
     */
    public final int incrementAndGet() {
        for (;;) {
            int current = get();
            int next = current + 1;
            if (compareAndSet(current, next))
                return next;
        }
    }

    /**
     * Atomically decrements by one the current value.
     *
     * @return the updated value
     */
    /**
     * 以原子方式将当前值减 1。
     * @return 更新的值
     */
    public final int decrementAndGet() {
        for (;;) {
            int current = get();
            int next = current - 1;
            if (compareAndSet(current, next))
                return next;
        }
    }

    /**
     * Atomically adds the given value to the current value.
     *
     * @param delta the value to add
     * @return the updated value
     */
    /**
     * 以原子方式将给定值与当前值相加。
     * @param  delta 要加上的值
     * @return       更新的值
     */
    public final int addAndGet(int delta) {
        for (;;) {
            int current = get();
            int next = current + delta;
            if (compareAndSet(current, next))
                return next;
        }
    }

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

    /**
     * 从类 Number 复制的描述 以 int 形式返回指定的数值。这可能会涉及到舍入或取整。
     * 指定者:类 Number 中的 intValue
     * @return 转换为 int 类型后该对象表示的数值。
     */
    public int intValue() {
    return get();
    }
    /**
     * 从类 Number 复制的描述 以 long 形式返回指定的数值。这可能涉及到舍入或取整。
     * 指定者:类 Number 中的 longValue
     * @return 转换为 long 类型后该对象表示的数值。
     */
    public long longValue() {
    return (long)get();
    }
    /**
     * 从类 Number 复制的描述 以 float 形式返回指定的数值。这可能会涉及到舍入。
     * 指定者:类 Number 中的 floatValue
     * @return 转换为 float 类型后该对象表示的数值。
     */
    public float floatValue() {
    return (float)get();
    }
    /**
     * 从类 Number 复制的描述 以 double 形式返回指定的数值。这可能会涉及到舍入。
     * 指定者:类 Number 中的 doubleValue
     * @return 转换为 double 类型后该对象表示的数值。
     */
    public double doubleValue() {
    return (double)get();
    }

}