Serializable
public class AtomicLong extends Number implements Serializable
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.Constructor | Description |
---|---|
AtomicLong() |
Creates a new AtomicLong with initial value 0 . |
AtomicLong |
Creates a new AtomicLong with the given initial value. |
Modifier and Type | Method | Description |
---|---|---|
final long |
accumulateAndGet |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) ) the current value with the results of applying the given function to the current and given values, returning the updated value. |
final long |
addAndGet |
Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final long |
compareAndExchange |
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(java.lang.Object...) . |
final long |
compareAndExchangeAcquire |
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(java.lang.Object...) . |
final long |
compareAndExchangeRelease |
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(java.lang.Object...) . |
final boolean |
compareAndSet |
Atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) . |
final long |
decrementAndGet() |
Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
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(java.lang.Object...) . |
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(java.lang.Object...) . |
final long |
get() |
Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
final long |
getAcquire() |
Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object...) . |
final long |
getAndAccumulate |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) ) the current value with the results of applying the given function to the current and given values, returning the previous value. |
final long |
getAndAdd |
Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final long |
getAndDecrement() |
Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final long |
getAndIncrement() |
Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final long |
getAndSet |
Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...) . |
final long |
getAndUpdate |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) ) the current value with the results of applying the given function, returning the previous value. |
final long |
getOpaque() |
Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object...) . |
final long |
getPlain() |
Returns the current value, with memory semantics of reading as if the variable was declared non- volatile . |
final long |
incrementAndGet() |
Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
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(java.lang.Object...) . |
final void |
lazySet |
Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object...) . |
long |
longValue() |
Returns the current value of this AtomicLong as a long , with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
final void |
set |
Sets the value to newValue , with memory effects as specified by VarHandle.setVolatile(java.lang.Object...) . |
final void |
setOpaque |
Sets the value to newValue , with memory effects as specified by VarHandle.setOpaque(java.lang.Object...) . |
final void |
setPlain |
Sets the value to newValue , with memory semantics of setting as if the variable was declared non-volatile and non-final . |
final void |
setRelease |
Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object...) . |
String |
toString() |
Returns the String representation of the current value. |
final long |
updateAndGet |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) ) the current value with the results of applying the given function, returning the updated value. |
final boolean |
weakCompareAndSet |
Deprecated. |
final boolean |
weakCompareAndSetAcquire |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...) . |
final boolean |
weakCompareAndSetPlain |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...) . |
final boolean |
weakCompareAndSetRelease |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...) . |
final boolean |
weakCompareAndSetVolatile |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...) . |
byteValue, shortValue
public AtomicLong(long initialValue)
initialValue
- the initial valuepublic AtomicLong()
0
.public final long get()
VarHandle.getVolatile(java.lang.Object...)
.public final void set(long newValue)
newValue
, with memory effects as specified by VarHandle.setVolatile(java.lang.Object...)
.newValue
- the new valuepublic final void lazySet(long newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final long getAndSet(long newValue)
newValue
and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...)
.newValue
- the new valuepublic final boolean compareAndSet(long expectedValue, long newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successful. False return indicates that the actual value was not equal to the expected value.@Deprecated(since="9") public final boolean weakCompareAndSet(long expectedValue, long newValue)
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.newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetPlain(long expectedValue, long newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final long getAndIncrement()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to getAndAdd(1)
.
public final long getAndDecrement()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to getAndAdd(-1)
.
public final long getAndAdd(long delta)
VarHandle.getAndAdd(java.lang.Object...)
.delta
- the value to addpublic final long incrementAndGet()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to addAndGet(1)
.
public final long decrementAndGet()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to addAndGet(-1)
.
public final long addAndGet(long delta)
VarHandle.getAndAdd(java.lang.Object...)
.delta
- the value to addpublic final long getAndUpdate(LongUnaryOperator updateFunction)
VarHandle.compareAndSet(java.lang.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.updateFunction
- a side-effect-free functionpublic final long updateAndGet(LongUnaryOperator updateFunction)
VarHandle.compareAndSet(java.lang.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.updateFunction
- a side-effect-free functionpublic final long getAndAccumulate(long x, LongBinaryOperator accumulatorFunction)
VarHandle.compareAndSet(java.lang.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.x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic final long accumulateAndGet(long x, LongBinaryOperator accumulatorFunction)
VarHandle.compareAndSet(java.lang.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.x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic String toString()
public int intValue()
AtomicLong
as an int
after a narrowing primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.intValue
in class Number
int
.public long longValue()
AtomicLong
as a long
, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
. Equivalent to get()
.public float floatValue()
AtomicLong
as a float
after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.floatValue
in class Number
float
.public double doubleValue()
AtomicLong
as a double
after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.doubleValue
in class Number
double
.public final long getPlain()
volatile
.public final void setPlain(long newValue)
newValue
, with memory semantics of setting as if the variable was declared non-volatile
and non-final
.newValue
- the new valuepublic final long getOpaque()
VarHandle.getOpaque(java.lang.Object...)
.public final void setOpaque(long newValue)
newValue
, with memory effects as specified by VarHandle.setOpaque(java.lang.Object...)
.newValue
- the new valuepublic final long getAcquire()
VarHandle.getAcquire(java.lang.Object...)
.public final void setRelease(long newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final long compareAndExchange(long expectedValue, long newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchange(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final long compareAndExchangeAcquire(long expectedValue, long newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeAcquire(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final long compareAndExchangeRelease(long expectedValue, long newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeRelease(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final boolean weakCompareAndSetVolatile(long expectedValue, long newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetAcquire(long expectedValue, long newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetRelease(long expectedValue, long newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successful
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/atomic/AtomicLong.html
compareAndExchange(long, long)
andcompareAndSet(long, long)
).