Added in API level 1

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(long initialValue)

Creates a new AtomicLong with the given initial value.

AtomicLong()

Creates a new AtomicLong with initial value 0.

Public methods

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.

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

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

Returns the String representation of the current value.

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.

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

Public constructors

AtomicLong

Added in API level 1
public AtomicLong (long initialValue)

Creates a new AtomicLong with the given initial value.

Parameters
initialValue long: the initial value

AtomicLong

Added in API level 1
public AtomicLong ()

Creates a new AtomicLong with initial value 0.

Public methods

accumulateAndGet

Added in API level 24
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

Added in API level 1
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
public final long get ()

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

Returns
long the current value

getAcquire

Added in API level 33
public final long getAcquire ()

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

Returns
long the value

getAndAccumulate

Added in API level 24
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

Added in API level 24
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

Added in API level 33
public final long getOpaque ()

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

Returns
long the value

getPlain

Added in API level 33
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

Added in API level 1
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

Added in API level 1
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

Added in API level 9
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

Added in API level 1
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

Added in API level 1
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 1
public String toString ()

Returns the String representation of the current value.

Returns
String the String representation of the current value

updateAndGet

Added in API level 24
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

Added in API level 1
Deprecated in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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