java.util.concurrent.atomic.AtomicLong

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

java.util.concurrent.atomic.AtomicLong

  1. private static native boolean VMSupportsCS8()
  2. public AtomicLong(long initialValue)
  3. public AtomicLong()
  4. public final long get()
  5. public final void set(long newValue)
  6. public final void lazySet(long newValue)
  7. public final long getAndSet(long newValue)
  8. public final boolean compareAndSet(long expect, long update)
  9. public final boolean weakCompareAndSet(long expect, long update)
  10. public final long getAndIncrement()
  11. public final long getAndDecrement()
  12. public final long getAndAdd(long delta)
  13. public final long incrementAndGet()
  14. public final long decrementAndGet()
  15. public final long addAndGet(long delta)
  16. public String toString()
  17. public int intValue()
  18. public long longValue()
  19. public float floatValue()
  20. 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;

/**
 * A {@code long} 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 AtomicLong} is used in applications such as atomically
 * incremented sequence numbers, and cannot be used as a replacement
 * for a {@link java.lang.Long}. 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
 */
/**
 * 可以用原子方式更新的 long 值。有关原子变量属性的描述,
 * 请参阅 java.util.concurrent.atomic 包规范。AtomicLong 可用在应用程序中
 * (如以原子方式增加的序列号),并且不能用于替换 Long。但是,此类确实扩展了 Number,
 * 允许那些处理基于数字类的工具和实用工具进行统一访问。
 */
public class AtomicLong extends Number implements java.io.Serializable {
    private static final long serialVersionUID = 1927816293512124184L;

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

    /**
     * Records whether the underlying JVM supports lockless
     * CompareAndSet for longs. While the unsafe.CompareAndSetLong
     * method works in either case, some constructions should be
     * handled at Java level to avoid locking user-visible locks.
     */
    static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();

    /**
     * Returns whether underlying JVM supports lockless CompareAndSet
     * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
     */
    private static native boolean VMSupportsCS8();

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

    private volatile long value;

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

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

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

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

    /**
     * Eventually sets to the given value.
     *
     * @param newValue the new value
     * @since 1.6
     */
    /**
     * 最后设置为给定值。
     * @param newValue 新值
     */
    public final void lazySet(long newValue) {
        unsafe.putOrderedLong(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 long getAndSet(long newValue) {
        while (true) {
            long 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(long expect, long update) {
    return unsafe.compareAndSwapLong(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(long expect, long update) {
    return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
    }

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

    /**
     * Atomically decrements by one the current value.
     *
     * @return the previous value
     */
    /**
     * 以原子方式将当前值减 1。
     * @return 以前的值
     */
    public final long getAndDecrement() {
        while (true) {
            long current = get();
            long 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 long getAndAdd(long delta) {
        while (true) {
            long current = get();
            long next = current + delta;
            if (compareAndSet(current, next))
                return current;
        }
    }

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

    /**
     * Atomically decrements by one the current value.
     *
     * @return the updated value
     */
    /**
     * 以原子方式将当前值减 1。
     * @return 更新的值
     */
    public final long decrementAndGet() {
        for (;;) {
            long current = get();
            long 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 long addAndGet(long delta) {
        for (;;) {
            long current = get();
            long 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.
     */
    /**
     * 返回当前值的字符串表示形式。
     * @return 当前值的字符串表示形式。
     */
    public String toString() {
        return Long.toString(get());
    }

    /**
     * 从类 Number 复制的描述
     * 以 int 形式返回指定的数值。这可能会涉及到舍入或取整。
     */
    public int intValue() {
    return (int)get();
    }
    /**
     * 从类 Number 复制的描述
     * 以 long 形式返回指定的数值。这可能涉及到舍入或取整。
     */
    public long longValue() {
    return (long)get();
    }
    /**
     * 从类 Number 复制的描述
     * 以 float 形式返回指定的数值。这可能会涉及到舍入。
     */
    public float floatValue() {
    return (float)get();
    }
    /**
     * 从类 Number 复制的描述
     * 以 double 形式返回指定的数值。这可能会涉及到舍入。
     */
    public double doubleValue() {
    return (double)get();
    }

}