模块  java.base

Class AtomicLong

  • 实现的所有接口
    Serializable

    public class AtomicLong
    extends Number
    implements Serializable
    可以原子方式更新的long值。 有关原子访问属性的描述,请参阅VarHandle规范。 AtomicLong用于诸如原子递增序列号的应用中,并且不能用作Long的替代 但是,此类确实扩展了Number以允许通过处理基于数字的类的工具和实用程序进行统一访问。
    从以下版本开始:
    1.5
    另请参见:
    Serialized Form
    • 构造方法详细信息

      • AtomicLong

        public AtomicLong​(long initialValue)
        使用给定的初始值创建一个新的AtomicLong。
        参数
        initialValue - 初始值
      • AtomicLong

        public AtomicLong()
        创建一个初始值为 0的新AtomicLong。
    • 方法详细信息

      • lazySet

        public final void lazySet​(long newValue)
        将值设置为 newValue ,具有 VarHandle.setRelease(java.lang.Object...)指定的记忆效果。
        参数
        newValue - 新值
        从以下版本开始:
        1.6
      • getAndSet

        public final long getAndSet​(long newValue)
        以原子方式将值设置为 newValue并返回旧值,并使用 VarHandle.getAndSet(java.lang.Object...)指定的内存效果。
        参数
        newValue - 新值
        结果
        以前的值
      • compareAndSet

        public final boolean compareAndSet​(long expectedValue,
                                           long newValue)
        原子将值设置为 newValue如果当前值 == expectedValue如通过指定,记忆效应 VarHandle.compareAndSet(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        true如果成功。 错误返回表示实际值不等于预期值。
      • weakCompareAndSetPlain

        public final boolean weakCompareAndSetPlain​(long expectedValue,
                                                    long newValue)
        可能原子将值设置为 newValue如果当前值 == expectedValue如通过指定,记忆效应 VarHandle.weakCompareAndSetPlain(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        true如果成功
        从以下版本开始:
        9
      • getAndIncrement

        public final long getAndIncrement()
        以原子方式递增当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。

        相当于getAndAdd(1)

        结果
        以前的值
      • getAndDecrement

        public final long getAndDecrement()
        原子地递减当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。

        相当于getAndAdd(-1)

        结果
        以前的值
      • getAndAdd

        public final long getAndAdd​(long delta)
        原子地将给定值添加到当前值,具有由 VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。
        参数
        delta - 要添加的值
        结果
        以前的值
      • incrementAndGet

        public final long incrementAndGet()
        以原子方式递增当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。

        相当于addAndGet(1)

        结果
        更新的值
      • decrementAndGet

        public final long decrementAndGet()
        原子地递减当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。

        相当于addAndGet(-1)

        结果
        更新的值
      • addAndGet

        public final long addAndGet​(long delta)
        原子地将给定值添加到当前值,具有由 VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。
        参数
        delta - 要添加的值
        结果
        更新的值
      • getAndUpdate

        public final long getAndUpdate​(LongUnaryOperator updateFunction)
        原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值和应用给定函数的结果,返回先前的值。 该函数应该是无副作用的,因为当尝试的更新由于线程之间的争用而失败时,它可能会被重新应用。
        参数
        updateFunction - 无副作用的功能
        结果
        以前的值
        从以下版本开始:
        1.8
      • updateAndGet

        public final long updateAndGet​(LongUnaryOperator updateFunction)
        原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值和应用给定函数的结果,返回更新的值。 该函数应该是无副作用的,因为当尝试的更新由于线程之间的争用而失败时,它可能会被重新应用。
        参数
        updateFunction - 无副作用的功能
        结果
        更新的值
        从以下版本开始:
        1.8
      • getAndAccumulate

        public final long getAndAccumulate​(long x,
                                           LongBinaryOperator accumulatorFunction)
        原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,返回先前值。 该函数应该是无副作用的,因为当尝试的更新由于线程之间的争用而失败时,它可能会被重新应用。 该函数应用当前值作为其第一个参数,并将给定更新作为第二个参数。
        参数
        x - 更新值
        accumulatorFunction - 两个参数的无副作用函数
        结果
        以前的值
        从以下版本开始:
        1.8
      • accumulateAndGet

        public final long accumulateAndGet​(long x,
                                           LongBinaryOperator accumulatorFunction)
        原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,返回更新的值。 该函数应该是无副作用的,因为当尝试的更新由于线程之间的争用而失败时,它可能会被重新应用。 该函数应用当前值作为其第一个参数,并将给定更新作为第二个参数。
        参数
        x - 更新值
        accumulatorFunction - 两个参数的无副作用函数
        结果
        更新的值
        从以下版本开始:
        1.8
      • toString

        public String toString()
        返回当前值的String表示形式。
        重写:
        toString在类 Object
        结果
        当前值的String表示形式
      • intValue

        public int intValue()
        在缩小基元转换后,返回 AtomicLong的当前值作为 int ,具有由 VarHandle.getVolatile(java.lang.Object...)指定的记忆效应。
        Specified by:
        intValue在类 Number
        结果
        转换为类型 int后此对象表示的数值。
        See The Java™ Language Specification:
        5.1.3缩小原始转换
      • floatValue

        public float floatValue()
        返回此的当前值 AtomicLong作为 float加宽原始转换后,通过规定的,具有记忆效果 VarHandle.getVolatile(java.lang.Object...)
        Specified by:
        floatValueNumber
        结果
        转换为类型 float后此对象表示的数值。
        See The Java™ Language Specification:
        5.1.2拓宽原始转换
      • doubleValue

        public double doubleValue()
        在扩展基元转换后,返回 AtomicLong的当前值,作为 double ,具有 VarHandle.getVolatile(java.lang.Object...)指定的记忆效应。
        Specified by:
        doubleValueNumber
        结果
        转换为类型 double后此对象表示的数值。
        See The Java™ Language Specification:
        5.1.2拓宽原始转换
      • getPlain

        public final long getPlain()
        返回当前值,读取的内存语义就像变量声明为非 volatile
        结果
        价值
        从以下版本开始:
        9
      • setPlain

        public final void setPlain​(long newValue)
        将值设置为 newValue ,设置的内存语义就像变量被声明为非 volatile和非 final
        参数
        newValue - 新值
        从以下版本开始:
        9
      • setOpaque

        public final void setOpaque​(long newValue)
        将值设置为 newValue ,具有 VarHandle.setOpaque(java.lang.Object...)指定的记忆效果。
        参数
        newValue - 新值
        从以下版本开始:
        9
      • setRelease

        public final void setRelease​(long newValue)
        将值设置为 newValue ,具有 VarHandle.setRelease(java.lang.Object...)指定的记忆效果。
        参数
        newValue - 新值
        从以下版本开始:
        9
      • compareAndExchange

        public final long compareAndExchange​(long expectedValue,
                                             long newValue)
        原子将值设置为 newValue如果当前值,被称为 证人值== expectedValue如通过指定,记忆效应 VarHandle.compareAndExchange(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        见证值,如果成功则与预期值相同
        从以下版本开始:
        9
      • compareAndExchangeAcquire

        public final long compareAndExchangeAcquire​(long expectedValue,
                                                    long newValue)
        原子将值设置为 newValue如果当前值,被称为 证人值== expectedValue如通过指定,记忆效应 VarHandle.compareAndExchangeAcquire(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        见证值,如果成功则与预期值相同
        从以下版本开始:
        9
      • compareAndExchangeRelease

        public final long compareAndExchangeRelease​(long expectedValue,
                                                    long newValue)
        原子将值设置为 newValue如果当前值,被称为 证人值== expectedValue如通过指定,记忆效应 VarHandle.compareAndExchangeRelease(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        见证值,如果成功则与预期值相同
        从以下版本开始:
        9
      • weakCompareAndSetVolatile

        public final boolean weakCompareAndSetVolatile​(long expectedValue,
                                                       long newValue)
        可能原子将值设置为 newValue如果当前值 == expectedValue如通过指定,记忆效应 VarHandle.weakCompareAndSet(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        true如果成功
        从以下版本开始:
        9
      • weakCompareAndSetAcquire

        public final boolean weakCompareAndSetAcquire​(long expectedValue,
                                                      long newValue)
        可能原子将值设置为 newValue如果当前值 == expectedValue如通过指定,记忆效应 VarHandle.weakCompareAndSetAcquire(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        true如果成功
        从以下版本开始:
        9
      • weakCompareAndSetRelease

        public final boolean weakCompareAndSetRelease​(long expectedValue,
                                                      long newValue)
        可能原子将值设置为 newValue如果当前值 == expectedValue如通过指定,记忆效应 VarHandle.weakCompareAndSetRelease(java.lang.Object...)
        参数
        expectedValue - 预期值
        newValue - 新值
        结果
        true如果成功
        从以下版本开始:
        9