AtomicLong
public
class
AtomicLong
extends Number
implements
Serializable
java.lang.Object | ||
↳ | java.lang.Number | |
↳ | java.util.concurrent.atomic.AtomicLong |
A long
value that may be updated atomically. See the
VarHandle
specification for descriptions of the properties
of atomic accesses. An AtomicLong
is used in applications
such as atomically incremented sequence numbers, and cannot be used
as a replacement for a Long
. However, this class
does extend Number
to allow uniform access by tools and
utilities that deal with numerically-based classes.
Summary
Public constructors | |
---|---|
AtomicLong()
Creates a new AtomicLong with initial value |
|
AtomicLong(long initialValue)
Creates a new AtomicLong with the given initial value. |
Public methods | |
---|---|
final
long
|
accumulateAndGet(long x, LongBinaryOperator accumulatorFunction)
Atomically updates (with memory effects as specified by |
final
long
|
addAndGet(long delta)
Atomically adds the given value to the current value,
with memory effects as specified by |
final
long
|
compareAndExchange(long expectedValue, long newValue)
Atomically sets the value to |
final
long
|
compareAndExchangeAcquire(long expectedValue, long newValue)
Atomically sets the value to |
final
long
|
compareAndExchangeRelease(long expectedValue, long newValue)
Atomically sets the value to |
final
boolean
|
compareAndSet(long expectedValue, long newValue)
Atomically sets the value to |
final
long
|
decrementAndGet()
Atomically decrements the current value,
with memory effects as specified by |
double
|
doubleValue()
Returns the current value of this |
float
|
floatValue()
Returns the current value of this |
final
long
|
get()
Returns the current value,
with memory effects as specified by |
final
long
|
getAcquire()
Returns the current value,
with memory effects as specified by |
final
long
|
getAndAccumulate(long x, LongBinaryOperator accumulatorFunction)
Atomically updates (with memory effects as specified by |
final
long
|
getAndAdd(long delta)
Atomically adds the given value to the current value,
with memory effects as specified by |
final
long
|
getAndDecrement()
Atomically decrements the current value,
with memory effects as specified by |
final
long
|
getAndIncrement()
Atomically increments the current value,
with memory effects as specified by |
final
long
|
getAndSet(long newValue)
Atomically sets the value to |
final
long
|
getAndUpdate(LongUnaryOperator updateFunction)
Atomically updates (with memory effects as specified by |
final
long
|
getOpaque()
Returns the current value,
with memory effects as specified by |
final
long
|
getPlain()
Returns the current value, with memory semantics of reading as if the
variable was declared non- |
final
long
|
incrementAndGet()
Atomically increments the current value,
with memory effects as specified by |
int
|
intValue()
Returns the current value of this |
final
void
|
lazySet(long newValue)
Sets the value to |
long
|
longValue()
Returns the current value of this |
final
void
|
set(long newValue)
Sets the value to |
final
void
|
setOpaque(long newValue)
Sets the value to |
final
void
|
setPlain(long newValue)
Sets the value to |
final
void
|
setRelease(long newValue)
Sets the value to |
String
|
toString()
Returns the String representation of the current value. |
final
long
|
updateAndGet(LongUnaryOperator updateFunction)
Atomically updates (with memory effects as specified by |
final
boolean
|
weakCompareAndSet(long expectedValue, long newValue)
This method was deprecated
in API level 33.
This method has plain memory effects but the method
name implies volatile memory effects (see methods such as
|
final
boolean
|
weakCompareAndSetAcquire(long expectedValue, long newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetPlain(long expectedValue, long newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetRelease(long expectedValue, long newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetVolatile(long expectedValue, long newValue)
Possibly atomically sets the value to |
Inherited methods | |
---|---|
Public constructors
AtomicLong
public AtomicLong (long initialValue)
Creates a new AtomicLong with the given initial value.
Parameters | |
---|---|
initialValue |
long : the initial value |
Public methods
accumulateAndGet
public final long accumulateAndGet (long x, LongBinaryOperator accumulatorFunction)
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)
) the current value with the results of
applying the given function to the current and given values,
returning the updated value. The function should be
side-effect-free, since it may be re-applied when attempted
updates fail due to contention among threads. The function is
applied with the current value as its first argument, and the
given update as the second argument.
Parameters | |
---|---|
x |
long : the update value |
accumulatorFunction |
LongBinaryOperator : a side-effect-free function of two arguments |
Returns | |
---|---|
long |
the updated value |
addAndGet
public final long addAndGet (long delta)
Atomically adds the given value to the current value,
with memory effects as specified by VarHandle.getAndAdd
.
Parameters | |
---|---|
delta |
long : the value to add |
Returns | |
---|---|
long |
the updated value |
compareAndExchange
public final long compareAndExchange (long expectedValue, long newValue)
Atomically sets the value to newValue
if the current value,
referred to as the witness value, == expectedValue
,
with memory effects as specified by
VarHandle.compareAndExchange
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
long |
the witness value, which will be the same as the expected value if successful |
compareAndExchangeAcquire
public final long compareAndExchangeAcquire (long expectedValue, long newValue)
Atomically sets the value to newValue
if the current value,
referred to as the witness value, == expectedValue
,
with memory effects as specified by
VarHandle.compareAndExchangeAcquire
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
long |
the witness value, which will be the same as the expected value if successful |
compareAndExchangeRelease
public final long compareAndExchangeRelease (long expectedValue, long newValue)
Atomically sets the value to newValue
if the current value,
referred to as the witness value, == expectedValue
,
with memory effects as specified by
VarHandle.compareAndExchangeRelease
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
long |
the witness value, which will be the same as the expected value if successful |
compareAndSet
public final boolean compareAndSet (long expectedValue, long newValue)
Atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by VarHandle.compareAndSet
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
boolean |
true if successful. False return indicates that
the actual value was not equal to the expected value. |
decrementAndGet
public final long decrementAndGet ()
Atomically decrements the current value,
with memory effects as specified by VarHandle.getAndAdd
.
Equivalent to addAndGet(-1)
.
Returns | |
---|---|
long |
the updated value |
doubleValue
public double doubleValue ()
Returns the current value of this AtomicLong
as a double
after a widening primitive conversion,
with memory effects as specified by VarHandle.getVolatile
.
Returns | |
---|---|
double |
the numeric value represented by this object after conversion
to type double . |
floatValue
public float floatValue ()
Returns the current value of this AtomicLong
as a float
after a widening primitive conversion,
with memory effects as specified by VarHandle.getVolatile
.
Returns | |
---|---|
float |
the numeric value represented by this object after conversion
to type float . |
get
public final long get ()
Returns the current value,
with memory effects as specified by VarHandle.getVolatile
.
Returns | |
---|---|
long |
the current value |
getAcquire
public final long getAcquire ()
Returns the current value,
with memory effects as specified by VarHandle.getAcquire
.
Returns | |
---|---|
long |
the value |
getAndAccumulate
public final long getAndAccumulate (long x, LongBinaryOperator accumulatorFunction)
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)
) the current value with the results of
applying the given function to the current and given values,
returning the previous value. The function should be
side-effect-free, since it may be re-applied when attempted
updates fail due to contention among threads. The function is
applied with the current value as its first argument, and the
given update as the second argument.
Parameters | |
---|---|
x |
long : the update value |
accumulatorFunction |
LongBinaryOperator : a side-effect-free function of two arguments |
Returns | |
---|---|
long |
the previous value |
getAndAdd
public final long getAndAdd (long delta)
Atomically adds the given value to the current value,
with memory effects as specified by VarHandle.getAndAdd
.
Parameters | |
---|---|
delta |
long : the value to add |
Returns | |
---|---|
long |
the previous value |
getAndDecrement
public final long getAndDecrement ()
Atomically decrements the current value,
with memory effects as specified by VarHandle.getAndAdd
.
Equivalent to getAndAdd(-1)
.
Returns | |
---|---|
long |
the previous value |
getAndIncrement
public final long getAndIncrement ()
Atomically increments the current value,
with memory effects as specified by VarHandle.getAndAdd
.
Equivalent to getAndAdd(1)
.
Returns | |
---|---|
long |
the previous value |
getAndSet
public final long getAndSet (long newValue)
Atomically sets the value to newValue
and returns the old value,
with memory effects as specified by VarHandle.getAndSet
.
Parameters | |
---|---|
newValue |
long : the new value |
Returns | |
---|---|
long |
the previous value |
getAndUpdate
public final long getAndUpdate (LongUnaryOperator updateFunction)
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)
) the current value with the results of
applying the given function, returning the previous value. The
function should be side-effect-free, since it may be re-applied
when attempted updates fail due to contention among threads.
Parameters | |
---|---|
updateFunction |
LongUnaryOperator : a side-effect-free function |
Returns | |
---|---|
long |
the previous value |
getOpaque
public final long getOpaque ()
Returns the current value,
with memory effects as specified by VarHandle.getOpaque
.
Returns | |
---|---|
long |
the value |
getPlain
public final long getPlain ()
Returns the current value, with memory semantics of reading as if the
variable was declared non-volatile
.
Returns | |
---|---|
long |
the value |
incrementAndGet
public final long incrementAndGet ()
Atomically increments the current value,
with memory effects as specified by VarHandle.getAndAdd
.
Equivalent to addAndGet(1)
.
Returns | |
---|---|
long |
the updated value |
intValue
public int intValue ()
Returns the current value of this AtomicLong
as an int
after a narrowing primitive conversion,
with memory effects as specified by VarHandle.getVolatile
.
Returns | |
---|---|
int |
the numeric value represented by this object after conversion
to type int . |
lazySet
public final void lazySet (long newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle.setRelease
.
Parameters | |
---|---|
newValue |
long : the new value |
longValue
public long longValue ()
Returns the current value of this AtomicLong
as a long
,
with memory effects as specified by VarHandle.getVolatile
.
Equivalent to get()
.
Returns | |
---|---|
long |
the numeric value represented by this object after conversion
to type long . |
set
public final void set (long newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle.setVolatile
.
Parameters | |
---|---|
newValue |
long : the new value |
setOpaque
public final void setOpaque (long newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle.setOpaque
.
Parameters | |
---|---|
newValue |
long : the new value |
setPlain
public final void setPlain (long newValue)
Sets the value to newValue
, with memory semantics
of setting as if the variable was declared non-volatile
and non-final
.
Parameters | |
---|---|
newValue |
long : the new value |
setRelease
public final void setRelease (long newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle.setRelease
.
Parameters | |
---|---|
newValue |
long : the new value |
toString
public String toString ()
Returns the String representation of the current value.
Returns | |
---|---|
String |
the String representation of the current value |
updateAndGet
public final long updateAndGet (LongUnaryOperator updateFunction)
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)
) the current value with the results of
applying the given function, returning the updated value. The
function should be side-effect-free, since it may be re-applied
when attempted updates fail due to contention among threads.
Parameters | |
---|---|
updateFunction |
LongUnaryOperator : a side-effect-free function |
Returns | |
---|---|
long |
the updated value |
weakCompareAndSet
public final boolean weakCompareAndSet (long expectedValue, long newValue)
This method was deprecated
in API level 33.
This method has plain memory effects but the method
name implies volatile memory effects (see methods such as
compareAndExchange(long, long)
and compareAndSet(long, long)
). To avoid
confusion over plain or volatile memory effects it is recommended that
the method weakCompareAndSetPlain(long, long)
be used instead.
Possibly atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by VarHandle.weakCompareAndSetPlain
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
boolean |
true if successful |
See also:
weakCompareAndSetAcquire
public final boolean weakCompareAndSetAcquire (long expectedValue, long newValue)
Possibly atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by
VarHandle.weakCompareAndSetAcquire
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
boolean |
true if successful |
weakCompareAndSetPlain
public final boolean weakCompareAndSetPlain (long expectedValue, long newValue)
Possibly atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by VarHandle.weakCompareAndSetPlain
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
boolean |
true if successful |
weakCompareAndSetRelease
public final boolean weakCompareAndSetRelease (long expectedValue, long newValue)
Possibly atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by
VarHandle.weakCompareAndSetRelease
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
boolean |
true if successful |
weakCompareAndSetVolatile
public final boolean weakCompareAndSetVolatile (long expectedValue, long newValue)
Possibly atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by
VarHandle.weakCompareAndSet
.
Parameters | |
---|---|
expectedValue |
long : the expected value |
newValue |
long : the new value |
Returns | |
---|---|
boolean |
true if successful |