Module java.base

Class AtomicInteger

java.lang.Object
java.lang.Number
java.util.concurrent.atomic.AtomicInteger
所有已实现的接口:
Serializable

public class AtomicInteger extends Number implements Serializable
一个可以原子方式更新的int值。查看VarHandle规范以了解原子访问属性的描述。一个AtomicInteger用于应用程序,例如原子递增计数器,并且不能用作Integer的替代品。但是,这个类扩展了Number,以允许工具和实用程序统一访问基于数字的类。
自JDK版本:
1.5
另请参阅:
  • Constructor Details

    • AtomicInteger

      public AtomicInteger(int initialValue)
      创建一个给定初始值的新AtomicInteger。
      参数:
      initialValue - 初始值
    • AtomicInteger

      public AtomicInteger()
      创建一个初始值为0的新AtomicInteger。
  • Method Details

    • get

      public final int get()
      返回当前值,使用VarHandle.getVolatile(java.lang.Object...)指定的内存效果。
      返回:
      当前值
    • set

      public final void set(int newValue)
      将值设置为newValue,使用VarHandle.setVolatile(java.lang.Object...)指定的内存效果。
      参数:
      newValue - 新值
    • lazySet

      public final void lazySet(int newValue)
      设置值为newValue,具有由VarHandle.setRelease(java.lang.Object...)指定的内存效果。
      参数:
      newValue - 新值
      自版本:
      1.6
    • getAndSet

      public final int getAndSet(int newValue)
      原子地将值设置为newValue并返回旧值,具有由VarHandle.getAndSet(java.lang.Object...)指定的内存效果。
      参数:
      newValue - 新值
      返回:
      先前的值
    • compareAndSet

      public final boolean compareAndSet(int expectedValue, int newValue)
      如果当前值== expectedValue,则以原子方式将值设置为newValue,具有由VarHandle.compareAndSet(java.lang.Object...)指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回:
      如果成功则返回true。返回false表示实际值不等于期望值。
    • weakCompareAndSet

      @Deprecated(since="9") public final boolean weakCompareAndSet(int expectedValue, int newValue)
      Deprecated.
      This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange(int, int) and compareAndSet(int, int)). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain(int, int) be used instead.
      如果当前值== expectedValue,则以可能原子方式将值设置为newValue,具有由VarHandle.weakCompareAndSetPlain(java.lang.Object...)指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回:
      如果成功则返回true
      参见:
    • weakCompareAndSetPlain

      public final boolean weakCompareAndSetPlain(int expectedValue, int newValue)
      如果当前值== expectedValue,则以可能原子方式将值设置为newValue,具有由VarHandle.weakCompareAndSetPlain(java.lang.Object...)指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回:
      如果成功则返回true
      自版本:
      9
    • getAndIncrement

      public final int getAndIncrement()
      原子地增加当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的内存效果。

      等同于getAndAdd(1)

      返回:
      先前的值
    • getAndDecrement

      public final int getAndDecrement()
      原子地减少当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的内存效果。

      等同于getAndAdd(-1)

      返回:
      先前的值
    • getAndAdd

      public final int getAndAdd(int delta)
      将给定值添加到当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的内存效果。
      参数:
      delta - 要添加的值
      返回:
      先前的值
    • incrementAndGet

      public final int incrementAndGet()
      原子地增加当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的内存效果。

      等同于addAndGet(1)

      返回:
      更新后的值
    • decrementAndGet

      public final int decrementAndGet()
      原子地减少当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的内存效果。

      等同于addAndGet(-1)

      返回:
      更新后的值
    • addAndGet

      public final int addAndGet(int delta)
      将给定值添加到当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的内存效果。
      参数:
      delta - 要添加的值
      返回:
      更新后的值
    • getAndUpdate

      public final int getAndUpdate(IntUnaryOperator updateFunction)
      原子地更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的内存效果)当前值,使用给定函数的结果返回先前的值。该函数应无副作用,因为在由于线程之间的争用而导致的尝试更新失败时,可能会重新应用该函数。
      参数:
      updateFunction - 无副作用的函数
      返回:
      先前的值
      自版本:
      1.8
    • updateAndGet

      public final int updateAndGet(IntUnaryOperator updateFunction)
      原子地更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的内存效果)当前值,使用给定函数的结果返回更新后的值。该函数应无副作用,因为在由于线程之间的争用而导致的尝试更新失败时,可能会重新应用该函数。
      参数:
      updateFunction - 无副作用的函数
      返回:
      更新后的值
      自版本:
      1.8
    • getAndAccumulate

      public final int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction)
      原子地更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的内存效果)当前值,使用将给定函数应用于当前值和给定值的结果返回先前的值。该函数应无副作用,因为在由于线程之间的争用而导致的尝试更新失败时,可能会重新应用该函数。该函数将当前值作为第一个参数,将给定更新作为第二个参数应用。
      参数:
      x - 更新值
      accumulatorFunction - 两个参数的无副作用函数
      返回:
      先前的值
      自版本:
      1.8
    • accumulateAndGet

      public final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction)
      原子地更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的内存效果)当前值,使用将给定函数应用于当前值和给定值的结果返回更新后的值。该函数应无副作用,因为在由于线程之间的争用而导致的尝试更新失败时,可能会重新应用该函数。该函数将当前值作为第一个参数,将给定更新作为第二个参数应用。
      参数:
      x - 更新值
      accumulatorFunction - 两个参数的无副作用函数
      返回:
      更新后的值
      自版本:
      1.8
    • toString

      public String toString()
      返回当前值的字符串表示形式。
      覆盖:
      toString 在类 Object
      返回:
      当前值的字符串表示形式
    • intValue

      public int intValue()
      int的形式返回此AtomicInteger的当前值,具有由VarHandle.getVolatile(java.lang.Object...)指定的内存效果。等同于get()
      指定者:
      intValue 在类 Number
      返回:
      转换为int类型后此对象表示的数值。
    • longValue

      public long longValue()
      以扩展原始转换后的long形式返回此AtomicInteger的当前值,具有由VarHandle.getVolatile(java.lang.Object...)指定的内存效果。
      指定者:
      longValue 在类 Number
      返回:
      转换为long类型后此对象表示的数值。
      参见 Java 语言规范:
      5.1.2 扩展原始转换
    • floatValue

      public float floatValue()
      以扩展原始转换后的float形式返回此AtomicInteger的当前值,具有由VarHandle.getVolatile(java.lang.Object...)指定的内存效果。
      指定者:
      floatValue 在类 Number
      返回:
      转换为float类型后此对象表示的数值。
      参见 Java 语言规范:
      5.1.2 扩展原始转换
    • doubleValue

      public double doubleValue()
      以扩展原始转换后的double形式返回此AtomicInteger的当前值,具有由VarHandle.getVolatile(java.lang.Object...)指定的内存效果。
      指定者:
      doubleValue 在类 Number
      返回值:
      转换为 double 类型后表示的数值。
      参见 Java 语言规范:
      5.1.2 扩展原始类型转换
    • getPlain

      public final int getPlain()
      返回当前值,具有读取内存语义,就好像变量被声明为非volatile
      返回值:
      该值
      自 JDK 版本:
      9
    • setPlain

      public final void setPlain(int newValue)
      将值设置为 newValue,具有设置内存语义,就好像变量被声明为非volatile和非final
      参数:
      newValue - 新值
      自 JDK 版本:
      9
    • getOpaque

      public final int getOpaque()
      返回当前值,具有由 VarHandle.getOpaque(java.lang.Object...) 指定的内存效果。
      返回值:
      该值
      自 JDK 版本:
      9
    • setOpaque

      public final void setOpaque(int newValue)
      将值设置为 newValue,具有由 VarHandle.setOpaque(java.lang.Object...) 指定的内存效果。
      参数:
      newValue - 新值
      自 JDK 版本:
      9
    • getAcquire

      public final int getAcquire()
      返回当前值,具有由 VarHandle.getAcquire(java.lang.Object...) 指定的内存效果。
      返回值:
      该值
      自 JDK 版本:
      9
    • setRelease

      public final void setRelease(int newValue)
      将值设置为 newValue,具有由 VarHandle.setRelease(java.lang.Object...) 指定的内存效果。
      参数:
      newValue - 新值
      自 JDK 版本:
      9
    • compareAndExchange

      public final int compareAndExchange(int expectedValue, int newValue)
      如果当前值(称为见证值== expectedValue,则将值原子地设置为 newValue,具有由 VarHandle.compareAndExchange(java.lang.Object...) 指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回值:
      见证值,如果成功将与期望值相同
      自 JDK 版本:
      9
    • compareAndExchangeAcquire

      public final int compareAndExchangeAcquire(int expectedValue, int newValue)
      如果当前值(称为见证值== expectedValue,则将值原子地设置为 newValue,具有由 VarHandle.compareAndExchangeAcquire(java.lang.Object...) 指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回值:
      见证值,如果成功将与期望值相同
      自 JDK 版本:
      9
    • compareAndExchangeRelease

      public final int compareAndExchangeRelease(int expectedValue, int newValue)
      如果当前值(称为见证值== expectedValue,则将值原子地设置为 newValue,具有由 VarHandle.compareAndExchangeRelease(java.lang.Object...) 指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回值:
      见证值,如果成功将与期望值相同
      自 JDK 版本:
      9
    • weakCompareAndSetVolatile

      public final boolean weakCompareAndSetVolatile(int expectedValue, int newValue)
      如果当前值 == expectedValue,则可能原子地将值设置为 newValue,具有由 VarHandle.weakCompareAndSet(java.lang.Object...) 指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回值:
      如果成功则为 true
      自 JDK 版本:
      9
    • weakCompareAndSetAcquire

      public final boolean weakCompareAndSetAcquire(int expectedValue, int newValue)
      如果当前值 == expectedValue,则可能原子地将值设置为 newValue,具有由 VarHandle.weakCompareAndSetAcquire(java.lang.Object...) 指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回值:
      如果成功则为 true
      自 JDK 版本:
      9
    • weakCompareAndSetRelease

      public final boolean weakCompareAndSetRelease(int expectedValue, int newValue)
      如果当前值 == expectedValue,则可能原子地将值设置为 newValue,具有由 VarHandle.weakCompareAndSetRelease(java.lang.Object...) 指定的内存效果。
      参数:
      expectedValue - 期望的值
      newValue - 新值
      返回值:
      如果成功则为 true
      自 JDK 版本:
      9