java.lang.Object
java.lang.Number
java.util.concurrent.atomic.AtomicInteger
- 所有已实现的接口:
Serializable
可以自动更新的
int 值。有关原子访问属性的描述,请参阅 VarHandle 规范。 AtomicInteger 用于原子递增计数器等应用,不能用作 Integer 的替代品。但是,此类确实扩展了 Number 以允许处理基于数字的类的工具和实用程序进行统一访问。
- 自从:
- 1.5
- 参见:
-
构造方法总结
构造方法 -
方法总结
修饰符和类型方法描述final intaccumulateAndGet(int x, IntBinaryOperator accumulatorFunction) 以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,并返回更新后的值。final intaddAndGet(int delta) 以原子方式将给定值添加到当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。final intcompareAndExchange(int expectedValue, int newValue) 如果当前值称为 witness value、== expectedValue,具有VarHandle.compareAndExchange(java.lang.Object...)指定的记忆效应,则原子地将值设置为newValue。final intcompareAndExchangeAcquire(int expectedValue, int newValue) 如果当前值称为 witness value、== expectedValue,具有VarHandle.compareAndExchangeAcquire(java.lang.Object...)指定的记忆效应,则原子地将值设置为newValue。final intcompareAndExchangeRelease(int expectedValue, int newValue) 如果当前值称为 witness value、== expectedValue,具有VarHandle.compareAndExchangeRelease(java.lang.Object...)指定的记忆效应,则原子地将值设置为newValue。final booleancompareAndSet(int expectedValue, int newValue) 如果当前值为== expectedValue,则以原子方式将值设置为newValue,并具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应。final int以原子方式递减当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。doublefloatfinal intget()返回当前值,具有VarHandle.getVolatile(java.lang.Object...)指定的记忆效应。final int返回当前值,具有VarHandle.getAcquire(java.lang.Object...)指定的记忆效应。final intgetAndAccumulate(int x, IntBinaryOperator accumulatorFunction) 以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,并返回先前的值。final intgetAndAdd(int delta) 以原子方式将给定值添加到当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。final int以原子方式递减当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。final int以原子方式递增当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。final intgetAndSet(int newValue) 以原子方式将值设置为newValue并返回旧值,具有VarHandle.getAndSet(java.lang.Object...)指定的记忆效应。final intgetAndUpdate(IntUnaryOperator updateFunction) 以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值与应用给定函数的结果,返回先前的值。final int返回当前值,具有VarHandle.getOpaque(java.lang.Object...)指定的记忆效应。final intgetPlain()返回当前值,具有读取的内存语义,就好像变量被声明为 non-volatile一样。final int以原子方式递增当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。intintValue()final voidlazySet(int newValue) 将值设置为newValue,具有VarHandle.setRelease(java.lang.Object...)指定的记忆效应。longfinal voidset(int newValue) 将值设置为newValue,具有VarHandle.setVolatile(java.lang.Object...)指定的记忆效应。final voidsetOpaque(int newValue) 将值设置为newValue,具有VarHandle.setOpaque(java.lang.Object...)指定的记忆效应。final voidsetPlain(int newValue) 将值设置为newValue,设置的内存语义就好像变量被声明为 non-volatile和 non-final一样。final voidsetRelease(int newValue) 将值设置为newValue,具有VarHandle.setRelease(java.lang.Object...)指定的记忆效应。toString()返回当前值的字符串表示形式。final intupdateAndGet(IntUnaryOperator updateFunction) 使用给定函数的应用结果以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值,返回更新后的值。final booleanweakCompareAndSet(int expectedValue, int newValue) 已弃用。final booleanweakCompareAndSetAcquire(int expectedValue, int newValue) 如果当前值为== expectedValue,则可能自动将该值设置为newValue,具有由VarHandle.weakCompareAndSetAcquire(java.lang.Object...)指定的记忆效应。final booleanweakCompareAndSetPlain(int expectedValue, int newValue) 如果当前值为== expectedValue,则可能自动将该值设置为newValue,并具有由VarHandle.weakCompareAndSetPlain(java.lang.Object...)指定的记忆效应。final booleanweakCompareAndSetRelease(int expectedValue, int newValue) 如果当前值为== expectedValue,则可能自动将该值设置为newValue,并具有由VarHandle.weakCompareAndSetRelease(java.lang.Object...)指定的记忆效应。final booleanweakCompareAndSetVolatile(int expectedValue, int newValue) 如果当前值为== expectedValue,则可能自动将该值设置为newValue,并具有由VarHandle.weakCompareAndSet(java.lang.Object...)指定的记忆效应。在类 java.lang.Number 中声明的方法
byteValue, shortValue
-
构造方法详细信息
-
AtomicInteger
public AtomicInteger(int initialValue) 创建具有给定初始值的新 AtomicInteger。- 参数:
initialValue- 初始值
-
AtomicInteger
public AtomicInteger()创建一个初始值为0的新 AtomicInteger。
-
-
方法详情
-
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如果成功。错误返回表示实际值不等于预期值。
-
weakCompareAndSet
已弃用。此方法具有简单的记忆效应,但方法名称暗示易失性记忆效应(请参阅compareAndExchange(int, int)和compareAndSet(int, int)等方法)。为避免混淆普通或易失性记忆效应,建议改用方法weakCompareAndSetPlain(int, int)。如果当前值为== 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
以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值与应用给定函数的结果,返回先前的值。该函数应该是无副作用的,因为当由于线程之间的争用而尝试更新失败时,它可能会被重新应用。- 参数:
updateFunction- 无副作用的函数- 返回:
- 以前的值
- 自从:
- 1.8
-
updateAndGet
使用给定函数的应用结果以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值,返回更新后的值。该函数应该是无副作用的,因为当由于线程之间的争用而尝试更新失败时,它可能会被重新应用。- 参数:
updateFunction- 无副作用的函数- 返回:
- 更新值
- 自从:
- 1.8
-
getAndAccumulate
以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,并返回先前的值。该函数应该是无副作用的,因为当由于线程之间的争用而尝试更新失败时,它可能会被重新应用。该函数应用当前值作为其第一个参数,给定的更新作为第二个参数。- 参数:
x- 更新值accumulatorFunction- 两个参数的无副作用函数- 返回:
- 以前的值
- 自从:
- 1.8
-
accumulateAndGet
以原子方式更新(具有VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,并返回更新后的值。该函数应该是无副作用的,因为当由于线程之间的争用而尝试更新失败时,它可能会被重新应用。该函数应用当前值作为其第一个参数,给定的更新作为第二个参数。- 参数:
x- 更新值accumulatorFunction- 两个参数的无副作用函数- 返回:
- 更新值
- 自从:
- 1.8
-
toString
返回当前值的字符串表示形式。 -
intValue
public int intValue() -
longValue
public long longValue()- 指定者:
longValue在类Number中- 返回:
-
转换为类型
long后此对象表示的数值。 - 看Java 语言规范:
-
5.1.2 拓宽原始转换
-
floatValue
public float floatValue()- 指定者:
floatValue在类Number中- 返回:
-
转换为类型
float后此对象表示的数值。 - 看Java 语言规范:
-
5.1.2 拓宽原始转换
-
doubleValue
public double doubleValue()- 指定者:
doubleValue在类Number中- 返回:
-
转换为类型
double后此对象表示的数值。 - 看Java 语言规范:
-
5.1.2 拓宽原始转换
-
getPlain
public final int getPlain()返回当前值,具有读取的内存语义,就好像变量被声明为 non-volatile一样。- 返回:
- 值
- 自从:
- 9
-
setPlain
public final void setPlain(int newValue) 将值设置为newValue,设置的内存语义就好像变量被声明为 non-volatile和 non-final一样。- 参数:
newValue- 新值- 自从:
- 9
-
getOpaque
public final int getOpaque()返回当前值,具有VarHandle.getOpaque(java.lang.Object...)指定的记忆效应。- 返回:
- 值
- 自从:
- 9
-
setOpaque
public final void setOpaque(int newValue) 将值设置为newValue,具有VarHandle.setOpaque(java.lang.Object...)指定的记忆效应。- 参数:
newValue- 新值- 自从:
- 9
-
getAcquire
public final int getAcquire()返回当前值,具有VarHandle.getAcquire(java.lang.Object...)指定的记忆效应。- 返回:
- 值
- 自从:
- 9
-
setRelease
public final void setRelease(int newValue) 将值设置为newValue,具有VarHandle.setRelease(java.lang.Object...)指定的记忆效应。- 参数:
newValue- 新值- 自从:
- 9
-
compareAndExchange
public final int compareAndExchange(int expectedValue, int newValue) 如果当前值称为 witness value、== expectedValue,具有VarHandle.compareAndExchange(java.lang.Object...)指定的记忆效应,则原子地将值设置为newValue。- 参数:
expectedValue- 期望值newValue- 新值- 返回:
- 见证值,如果成功,它将与预期值相同
- 自从:
- 9
-
compareAndExchangeAcquire
public final int compareAndExchangeAcquire(int expectedValue, int newValue) 如果当前值称为 witness value、== expectedValue,具有VarHandle.compareAndExchangeAcquire(java.lang.Object...)指定的记忆效应,则原子地将值设置为newValue。- 参数:
expectedValue- 期望值newValue- 新值- 返回:
- 见证值,如果成功,它将与预期值相同
- 自从:
- 9
-
compareAndExchangeRelease
public final int compareAndExchangeRelease(int expectedValue, int newValue) 如果当前值称为 witness value、== expectedValue,具有VarHandle.compareAndExchangeRelease(java.lang.Object...)指定的记忆效应,则原子地将值设置为newValue。- 参数:
expectedValue- 期望值newValue- 新值- 返回:
- 见证值,如果成功,它将与预期值相同
- 自从:
- 9
-
weakCompareAndSetVolatile
public final boolean weakCompareAndSetVolatile(int expectedValue, int newValue) 如果当前值为== expectedValue,则可能自动将该值设置为newValue,并具有由VarHandle.weakCompareAndSet(java.lang.Object...)指定的记忆效应。- 参数:
expectedValue- 期望值newValue- 新值- 返回:
true如果成功- 自从:
- 9
-
weakCompareAndSetAcquire
public final boolean weakCompareAndSetAcquire(int expectedValue, int newValue) 如果当前值为== expectedValue,则可能自动将该值设置为newValue,具有由VarHandle.weakCompareAndSetAcquire(java.lang.Object...)指定的记忆效应。- 参数:
expectedValue- 期望值newValue- 新值- 返回:
true如果成功- 自从:
- 9
-
weakCompareAndSetRelease
public final boolean weakCompareAndSetRelease(int expectedValue, int newValue) 如果当前值为== expectedValue,则可能自动将该值设置为newValue,并具有由VarHandle.weakCompareAndSetRelease(java.lang.Object...)指定的记忆效应。- 参数:
expectedValue- 期望值newValue- 新值- 返回:
true如果成功- 自从:
- 9
-
compareAndExchange(int, int)和compareAndSet(int, int)等方法)。