Google is committed to advancing racial equity for Black communities. See how.
Added in API level 1

AtomicLong

open class AtomicLong : Number, Serializable
kotlin.Any
   ↳ kotlin.Number
   ↳ java.util.concurrent.atomic.AtomicLong

A long value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. 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.

Summary

Public constructors

<init>(initialValue: Long)

Creates a new AtomicLong with the given initial value.

Creates a new AtomicLong with initial value 0.

Public methods

Long
accumulateAndGet(x: Long, accumulatorFunction: LongBinaryOperator!)

Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.

Long
addAndGet(delta: Long)

Atomically adds the given value to the current value.

Boolean
compareAndSet(expect: Long, update: Long)

Atomically sets the value to the given updated value if the current value == the expected value.

Long

Atomically decrements by one the current value.

Long
get()

Gets the current value.

Long
getAndAccumulate(x: Long, accumulatorFunction: LongBinaryOperator!)

Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.

Long
getAndAdd(delta: Long)

Atomically adds the given value to the current value.

Long

Atomically decrements by one the current value.

Long

Atomically increments by one the current value.

Long
getAndSet(newValue: Long)

Atomically sets to the given value and returns the old value.

Long
getAndUpdate(updateFunction: LongUnaryOperator!)

Atomically updates the current value with the results of applying the given function, returning the previous value.

Long

Atomically increments by one the current value.

Unit
lazySet(newValue: Long)

Eventually sets to the given value.

Unit
set(newValue: Long)

Sets to the given value.

open Double

Returns the value of this AtomicLong as a double after a widening primitive conversion.

open Float

Returns the value of this AtomicLong as a float after a widening primitive conversion.

open Int

Returns the value of this AtomicLong as an int after a narrowing primitive conversion.

open Long

Returns the value of this AtomicLong as a long.

open String

Returns the String representation of the current value.

Long
updateAndGet(updateFunction: LongUnaryOperator!)

Atomically updates the current value with the results of applying the given function, returning the updated value.

Boolean
weakCompareAndSet(expect: Long, update: Long)

Atomically sets the value to the given updated value if the current value == the expected value.

Public constructors

<init>

Added in API level 1
AtomicLong(initialValue: Long)

Creates a new AtomicLong with the given initial value.

Parameters
initialValue Long: the initial value

<init>

Added in API level 1
AtomicLong()

Creates a new AtomicLong with initial value 0.

Public methods

accumulateAndGet

Added in API level 24
fun accumulateAndGet(
    x: Long,
    accumulatorFunction: LongBinaryOperator!
): Long

Atomically updates 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
Return
Long the updated value

addAndGet

Added in API level 1
fun addAndGet(delta: Long): Long

Atomically adds the given value to the current value.

Parameters
delta Long: the value to add
Return
Long the updated value

compareAndSet

Added in API level 1
fun compareAndSet(
    expect: Long,
    update: Long
): Boolean

Atomically sets the value to the given updated value if the current value == the expected value.

Parameters
expect Long: the expected value
update Long: the new value
Return
Boolean true if successful. False return indicates that the actual value was not equal to the expected value.

decrementAndGet

Added in API level 1
fun decrementAndGet(): Long

Atomically decrements by one the current value.

Return
Long the updated value

get

Added in API level 1
fun get(): Long

Gets the current value.

Return
Long the current value

getAndAccumulate

Added in API level 24
fun getAndAccumulate(
    x: Long,
    accumulatorFunction: LongBinaryOperator!
): Long

Atomically updates 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
Return
Long the previous value

getAndAdd

Added in API level 1
fun getAndAdd(delta: Long): Long

Atomically adds the given value to the current value.

Parameters
delta Long: the value to add
Return
Long the previous value

getAndDecrement

Added in API level 1
fun getAndDecrement(): Long

Atomically decrements by one the current value.

Return
Long the previous value

getAndIncrement

Added in API level 1
fun getAndIncrement(): Long

Atomically increments by one the current value.

Return
Long the previous value

getAndSet

Added in API level 1
fun getAndSet(newValue: Long): Long

Atomically sets to the given value and returns the old value.

Parameters
newValue Long: the new value
Return
Long the previous value

getAndUpdate

Added in API level 24
fun getAndUpdate(updateFunction: LongUnaryOperator!): Long

Atomically updates 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
Return
Long the previous value

incrementAndGet

Added in API level 1
fun incrementAndGet(): Long

Atomically increments by one the current value.

Return
Long the updated value

lazySet

Added in API level 9
fun lazySet(newValue: Long): Unit

Eventually sets to the given value.

Parameters
newValue Long: the new value

set

Added in API level 1
fun set(newValue: Long): Unit

Sets to the given value.

Parameters
newValue Long: the new value

toDouble

Added in API level 1
open fun toDouble(): Double

Returns the value of this AtomicLong as a double after a widening primitive conversion.

Return
Double the numeric value represented by this object after conversion to type double.

toFloat

Added in API level 1
open fun toFloat(): Float

Returns the value of this AtomicLong as a float after a widening primitive conversion.

Return
Float the numeric value represented by this object after conversion to type float.

toInt

Added in API level 1
open fun toInt(): Int

Returns the value of this AtomicLong as an int after a narrowing primitive conversion.

Return
Int the numeric value represented by this object after conversion to type int.

toLong

Added in API level 1
open fun toLong(): Long

Returns the value of this AtomicLong as a long. Equivalent to get().

Return
Long the numeric value represented by this object after conversion to type long.

toString

Added in API level 1
open fun toString(): String

Returns the String representation of the current value.

Return
String the String representation of the current value

updateAndGet

Added in API level 24
fun updateAndGet(updateFunction: LongUnaryOperator!): Long

Atomically updates 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
Return
Long the updated value

weakCompareAndSet

Added in API level 1
fun weakCompareAndSet(
    expect: Long,
    update: Long
): Boolean

Atomically sets the value to the given updated value if the current value == the expected value.

May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.

Parameters
expect Long: the expected value
update Long: the new value
Return
Boolean true if successful