- java.lang.Object
-
- java.lang.Number
-
- java.util.concurrent.atomic.AtomicLong
-
- 实现的所有接口
-
Serializable
public class AtomicLong extends Number implements Serializable
可以原子方式更新的long值。 有关原子访问属性的描述,请参阅VarHandle规范。AtomicLong用于诸如原子递增序列号的应用中,并且不能用作Long的替代物 。 但是,此类确实扩展了Number以允许通过处理基于数字的类的工具和实用程序进行统一访问。- 从以下版本开始:
- 1.5
- 另请参见:
- Serialized Form
-
-
构造方法摘要
构造方法 构造器 描述 AtomicLong()创建一个初始值为0的新AtomicLong。AtomicLong(long initialValue)使用给定的初始值创建一个新的AtomicLong。
-
方法摘要
所有方法 实例方法 具体的方法 弃用的方法 变量和类型 方法 描述 longaccumulateAndGet(long x, LongBinaryOperator accumulatorFunction)原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,返回更新的值。longaddAndGet(long delta)原子地将给定值添加到当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。longcompareAndExchange(long expectedValue, long newValue)原子将值设置为newValue如果当前值,被称为 证人值 ,== expectedValue如通过指定,记忆效应VarHandle.compareAndExchange(java.lang.Object...)。longcompareAndExchangeAcquire(long expectedValue, long newValue)原子将值设置为newValue如果当前值,被称为 证人值 ,== expectedValue如通过指定,记忆效应VarHandle.compareAndExchangeAcquire(java.lang.Object...)。longcompareAndExchangeRelease(long expectedValue, long newValue)原子将值设置为newValue如果当前值,被称为 证人值 ,== expectedValue如通过指定,记忆效应VarHandle.compareAndExchangeRelease(java.lang.Object...)。booleancompareAndSet(long expectedValue, long newValue)longdecrementAndGet()原子地递减当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。doubledoubleValue()floatfloatValue()longget()返回当前值,具有VarHandle.getVolatile(java.lang.Object...)指定的内存效果。longgetAcquire()返回当前值,具有VarHandle.getAcquire(java.lang.Object...)指定的内存效果。longgetAndAccumulate(long x, LongBinaryOperator accumulatorFunction)原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值以及将给定函数应用于当前值和给定值的结果,返回先前的值。longgetAndAdd(long delta)原子地将给定值添加到当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。longgetAndDecrement()原子地递减当前值,具有由VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。longgetAndIncrement()以原子方式递增当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。longgetAndSet(long newValue)以原子方式将值设置为newValue并返回旧值,并使用VarHandle.getAndSet(java.lang.Object...)指定的内存效果。longgetAndUpdate(LongUnaryOperator updateFunction)原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值和应用给定函数的结果,返回先前的值。longgetOpaque()返回当前值,具有VarHandle.getOpaque(java.lang.Object...)指定的内存效果。longgetPlain()返回当前值,读取的内存语义就像变量声明为非volatile。longincrementAndGet()以原子方式递增当前值,具有VarHandle.getAndAdd(java.lang.Object...)指定的记忆效应。intintValue()voidlazySet(long newValue)将值设置为newValue,具有VarHandle.setRelease(java.lang.Object...)指定的记忆效果。longlongValue()voidset(long newValue)将值设置为newValue,具有VarHandle.setVolatile(java.lang.Object...)指定的记忆效果。voidsetOpaque(long newValue)将值设置为newValue,具有VarHandle.setOpaque(java.lang.Object...)指定的记忆效果。voidsetPlain(long newValue)将值设置为newValue,设置的内存语义就像变量被声明为非volatile和非final。voidsetRelease(long newValue)将值设置为newValue,具有VarHandle.setRelease(java.lang.Object...)指定的记忆效果。StringtoString()返回当前值的String表示形式。longupdateAndGet(LongUnaryOperator updateFunction)原子更新(具有由VarHandle.compareAndSet(java.lang.Object...)指定的记忆效应)当前值和应用给定函数的结果,返回更新的值。booleanweakCompareAndSet(long expectedValue, long newValue)已过时。此方法具有简单的记忆效应,但方法名称意味着易失性记忆效应(请参阅compareAndExchange(long, long)和compareAndSet(long, long)等方法)。booleanweakCompareAndSetAcquire(long expectedValue, long newValue)可能原子将值设置为newValue如果当前值== expectedValue如通过指定,记忆效应VarHandle.weakCompareAndSetAcquire(java.lang.Object...)。booleanweakCompareAndSetPlain(long expectedValue, long newValue)可能原子将值设置为newValue如果当前值== expectedValue如通过指定,记忆效应VarHandle.weakCompareAndSetPlain(java.lang.Object...)。booleanweakCompareAndSetRelease(long expectedValue, long newValue)可能原子将值设置为newValue如果当前值== expectedValue如通过指定,记忆效应VarHandle.weakCompareAndSetRelease(java.lang.Object...)。booleanweakCompareAndSetVolatile(long expectedValue, long newValue)可能原子将值设置为newValue如果当前值== expectedValue如通过指定,记忆效应VarHandle.weakCompareAndSet(java.lang.Object...)。-
声明方法的类 java.lang.Number
byteValue, shortValue
-
-
-
-
方法详细信息
-
get
public final long get()
返回当前值,具有VarHandle.getVolatile(java.lang.Object...)指定的内存效果。- 结果
- 当前的价值
-
set
public final void set(long newValue)
将值设置为newValue,具有VarHandle.setVolatile(java.lang.Object...)指定的记忆效果。- 参数
-
newValue- 新值
-
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)- 参数
-
expectedValue- 预期值 -
newValue- 新值 - 结果
-
true如果成功。 错误返回表示实际值不等于预期值。
-
weakCompareAndSet
@Deprecated(since="9") public final boolean weakCompareAndSet(long expectedValue, long newValue)
Deprecated.This method has plain memory effects but the method name implies volatile memory effects (see methods such ascompareAndExchange(long, long)andcompareAndSet(long, long)). To avoid confusion over plain or volatile memory effects it is recommended that the methodweakCompareAndSetPlain(long, long)be used instead.可能原子将值设置为newValue如果当前值== expectedValue如通过指定,记忆效应VarHandle.weakCompareAndSetPlain(java.lang.Object...)。- 参数
-
expectedValue- 预期值 -
newValue- 新值 - 结果
-
true如果成功 - 另请参见:
-
weakCompareAndSetPlain(long, long)
-
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
-
intValue
public int intValue()
-
longValue
public long longValue()
返回此的当前值AtomicLong作为long如通过指定,记忆效应VarHandle.getVolatile(java.lang.Object...)。 相当于get()。
-
floatValue
public float floatValue()
- Specified by:
-
floatValue类Number - 结果
-
转换为类型
float后此对象表示的数值。 - See The Java™ Language Specification:
- 5.1.2拓宽原始转换
-
doubleValue
public double doubleValue()
- Specified by:
-
doubleValue类Number - 结果
-
转换为类型
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
-
getOpaque
public final long getOpaque()
返回当前值,具有VarHandle.getOpaque(java.lang.Object...)指定的内存效果。- 结果
- 价值
- 从以下版本开始:
- 9
-
setOpaque
public final void setOpaque(long newValue)
将值设置为newValue,具有VarHandle.setOpaque(java.lang.Object...)指定的记忆效果。- 参数
-
newValue- 新值 - 从以下版本开始:
- 9
-
getAcquire
public final long getAcquire()
返回当前值,具有VarHandle.getAcquire(java.lang.Object...)指定的内存效果。- 结果
- 价值
- 从以下版本开始:
- 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
-
-