AtomicLong  |  API reference  |  Android Developers

public class AtomicLong
extends Number implements Serializable



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 0.

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 VarHandle.compareAndSet) the current value with the results of applying the given function to the current and given values, returning the updated value.

final long addAndGet(long delta)

Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd.

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.

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.

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.

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.

final long decrementAndGet()

Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd.

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.

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.

final long get()

Returns the current value, with memory effects as specified by VarHandle.getVolatile.

final long getAcquire()

Returns the current value, with memory effects as specified by VarHandle.getAcquire.

final long getAndAccumulate(long x, LongBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) the current value with the results of applying the given function to the current and given values, returning the previous value.

final long getAndAdd(long delta)

Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd.

final long getAndDecrement()

Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd.

final long getAndIncrement()

Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd.

final long getAndSet(long newValue)

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet.

final long getAndUpdate(LongUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) 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.

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.

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.

final void lazySet(long newValue)

Sets the value to newValue, with memory effects as specified by VarHandle.setRelease.

long longValue()

Returns the current value of this AtomicLong as a long, with memory effects as specified by VarHandle.getVolatile.

final void set(long newValue)

Sets the value to newValue, with memory effects as specified by VarHandle.setVolatile.

final void setOpaque(long newValue)

Sets the value to newValue, with memory effects as specified by VarHandle.setOpaque.

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.

final void setRelease(long newValue)

Sets the value to newValue, with memory effects as specified by VarHandle.setRelease.

String toString()
final long updateAndGet(LongUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) the current value with the results of applying the given function, returning the updated value.

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.

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.

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.

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.

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.

Inherited methods

From class java.lang.Number

byte byteValue()

Returns the value of the specified number as a byte.

abstract double doubleValue()

Returns the value of the specified number as a double.

abstract float floatValue()

Returns the value of the specified number as a float.

abstract int intValue()

Returns the value of the specified number as an int.

abstract long longValue()

Returns the value of the specified number as a long.

short shortValue()

Returns the value of the specified number as a short.

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

AtomicLong

public AtomicLong ()

Creates a new AtomicLong with initial value 0.

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) 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) 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) 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
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) 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

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