APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.concurrent.atomic

public Class AtomicLong

extends Number
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.lang.invoke.VarHandle, java.util.function.LongBinaryOperator, .LongUnaryOperator, jdk.internal.misc.Unsafe

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 java.lang.Long. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.
Author
Doug Lea
Since
1.5

Field Summary

Modifier and TypeField and Description
private static final long
private static final Unsafe
U

private static final long
private volatile long

Constructor Summary

AccessConstructor and Description
public
AtomicLong(long
the initial value
initialValue
)

Creates a new AtomicLong with the given initial value.

public
AtomicLong()

Creates a new AtomicLong with initial value 0.

Method Summary

Modifier and TypeMethod and Description
public final long

Returns:

the updated value
accumulateAndGet
(long
the update value
x
,
LongBinaryOperator
a side-effect-free function of two arguments
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.

public final long

Returns:

the updated value
addAndGet
(long
the value to add
delta
)

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

public final long

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchange
(long
the expected value
expectedValue
,
long
the new value
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.

public final long

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeAcquire
(long
the expected value
expectedValue
,
long
the new value
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.

public final long

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeRelease
(long
the expected value
expectedValue
,
long
the new value
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.

public final boolean

Returns:

true if successful. False return indicates that the actual value was not equal to the expected value.
compareAndSet
(long
the expected value
expectedValue
,
long
the new value
newValue
)

Atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#compareAndSet.

public final long

Returns:

the updated value
decrementAndGet
()

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

public double
doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the current value of this AtomicLong as a double after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

public float
floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the current value of this AtomicLong as a float after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

public final long

Returns:

the current value
get
()

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

public final long

Returns:

the value
getAcquire
()

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

public final long

Returns:

the previous value
getAndAccumulate
(long
the update value
x
,
LongBinaryOperator
a side-effect-free function of two arguments
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.

public final long

Returns:

the previous value
getAndAdd
(long
the value to add
delta
)

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

public final long

Returns:

the previous value
getAndDecrement
()

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

public final long

Returns:

the previous value
getAndIncrement
()

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

public final long

Returns:

the previous value
getAndSet
(long
the new value
newValue
)

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

public final long

Returns:

the previous value
getAndUpdate
(LongUnaryOperator
a side-effect-free function
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.

public final long

Returns:

the value
getOpaque
()

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

public final long

Returns:

the value
getPlain
()

Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.

public final long

Returns:

the updated value
incrementAndGet
()

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

public int
intValue()

Implements abstract java.lang.Number.intValue.

Returns the current value of this AtomicLong as an int after a narrowing primitive conversion, with memory effects as specified by VarHandle#getVolatile.

public final void
lazySet(long
the new value
newValue
)

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

public long
longValue()

Implements abstract java.lang.Number.longValue.

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

public final void
set(long
the new value
newValue
)

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

public final void
setOpaque(long
the new value
newValue
)

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

public final void
setPlain(long
the new value
newValue
)

Sets the value to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.

public final void
setRelease(long
the new value
newValue
)

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

public String

Returns:

the String representation of the current value
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

public final long

Returns:

the updated value
updateAndGet
(LongUnaryOperator
a side-effect-free function
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.

public final boolean

Returns:

true if successful
weakCompareAndSet
(long
the expected value
expectedValue
,
long
the new value
newValue
)
Deprecated since 9. This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet).

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

public final boolean

Returns:

true if successful
weakCompareAndSetAcquire
(long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetAcquire.

public final boolean

Returns:

true if successful
weakCompareAndSetPlain
(long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

public final boolean

Returns:

true if successful
weakCompareAndSetRelease
(long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetRelease.

public final boolean

Returns:

true if successful
weakCompareAndSetVolatile
(long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSet.

Inherited from java.lang.Number:
byteValueshortValue

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Number.serialVersionUID.

Uback to summary
private static final Unsafe U
VALUEback to summary
private static final long VALUE
valueback to summary
private volatile long value

Constructor Detail

AtomicLongback to summary
public AtomicLong(long initialValue)

Creates a new AtomicLong with the given initial value.

Parameters
initialValue:long

the initial value

AtomicLongback to summary
public AtomicLong()

Creates a new AtomicLong with initial value 0.

Method Detail

accumulateAndGetback to summary
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

Since
1.8
addAndGetback to summary
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

compareAndExchangeback to summary
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

Since
9
compareAndExchangeAcquireback to summary
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

Since
9
compareAndExchangeReleaseback to summary
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

Since
9
compareAndSetback to summary
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.

decrementAndGetback to summary
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

doubleValueback to summary
public double doubleValue()

Implements abstract java.lang.Number.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 Language Specification
5.1.2 Widening Primitive Conversion
floatValueback to summary
public float floatValue()

Implements abstract java.lang.Number.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 Language Specification
5.1.2 Widening Primitive Conversion
getback to summary
public final long get()

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

Returns:long

the current value

getAcquireback to summary
public final long getAcquire()

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

Returns:long

the value

Since
9
getAndAccumulateback to summary
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

Since
1.8
getAndAddback to summary
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

getAndDecrementback to summary
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

getAndIncrementback to summary
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

getAndSetback to summary
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

getAndUpdateback to summary
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

Since
1.8
getOpaqueback to summary
public final long getOpaque()

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

Returns:long

the value

Since
9
getPlainback to summary
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

Since
9
incrementAndGetback to summary
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

intValueback to summary
public int intValue()

Implements abstract java.lang.Number.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 Language Specification
5.1.3 Narrowing Primitive Conversion
lazySetback to summary
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

Since
1.6
longValueback to summary
public long longValue()

Implements abstract java.lang.Number.longValue.

Returns the current value of this AtomicLong as a long, with memory effects as specified by VarHandle#getVolatile. Equivalent to get().

setback to summary
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

setOpaqueback to summary
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

Since
9
setPlainback to summary
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

Since
9
setReleaseback to summary
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

Since
9
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

Returns:String

the String representation of the current value

updateAndGetback to summary
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

Since
1.8
weakCompareAndSetback to summary
public final boolean weakCompareAndSet(long expectedValue, long newValue)

Deprecated

since 9.

This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain 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

Annotations
@Deprecated
since:9
See Also
weakCompareAndSetPlain
weakCompareAndSetAcquireback to summary
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

Since
9
weakCompareAndSetPlainback to summary
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

Since
9
weakCompareAndSetReleaseback to summary
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

Since
9
weakCompareAndSetVolatileback to summary
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

Since
9