Added in API level 1

AtomicInteger

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

An int value that may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses. An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an java.lang.Integer. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.

Summary

Public constructors
AtomicInteger(initialValue: Int)

Creates a new AtomicInteger with the given initial value.

Creates a new AtomicInteger with initial value 0.

Public methods
Int
accumulateAndGet(x: Int, accumulatorFunction: IntBinaryOperator!)

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

Int
addAndGet(delta: Int)

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

Int
compareAndExchange(expectedValue: Int, newValue: Int)

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.

Int
compareAndExchangeAcquire(expectedValue: Int, newValue: Int)

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.

Int
compareAndExchangeRelease(expectedValue: Int, newValue: Int)

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.

Boolean
compareAndSet(expectedValue: Int, newValue: Int)

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

Int

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

Int
get()

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

Int

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

Int
getAndAccumulate(x: Int, accumulatorFunction: IntBinaryOperator!)

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

Int
getAndAdd(delta: Int)

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

Int

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

Int

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

Int
getAndSet(newValue: Int)

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

Int
getAndUpdate(updateFunction: IntUnaryOperator!)

Atomically updates (with memory effects as specified by java.lang.invoke.VarHandle#compareAndSet) the current value with the results of applying the given function, returning the previous value.

Int

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

Int

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

Int

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

Unit
lazySet(newValue: Int)

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

Unit
set(newValue: Int)

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

Unit
setOpaque(newValue: Int)

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

Unit
setPlain(newValue: Int)

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

Unit
setRelease(newValue: Int)

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

open Double

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

open Float

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

open Int

Returns the current value of this AtomicInteger as an int, with memory effects as specified by VarHandle#getVolatile.

open Long

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

open String

Returns the String representation of the current value.

Int
updateAndGet(updateFunction: IntUnaryOperator!)

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

Boolean
weakCompareAndSet(expectedValue: Int, newValue: Int)

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

Boolean
weakCompareAndSetAcquire(expectedValue: Int, newValue: Int)

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

Boolean
weakCompareAndSetPlain(expectedValue: Int, newValue: Int)

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

Boolean
weakCompareAndSetRelease(expectedValue: Int, newValue: Int)

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

Boolean
weakCompareAndSetVolatile(expectedValue: Int, newValue: Int)

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

Public constructors

AtomicInteger

Added in API level 1
AtomicInteger(initialValue: Int)

Creates a new AtomicInteger with the given initial value.

Parameters
initialValue Int: the initial value

AtomicInteger

Added in API level 1
AtomicInteger()

Creates a new AtomicInteger with initial value 0.

Public methods

accumulateAndGet

Added in API level 24
fun accumulateAndGet(
    x: Int,
    accumulatorFunction: IntBinaryOperator!
): Int

Atomically updates (with memory effects as specified by java.lang.invoke.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 Int: the update value
accumulatorFunction IntBinaryOperator!: a side-effect-free function of two arguments
Return
Int the updated value

addAndGet

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

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

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

compareAndExchange

Added in API level 33
fun compareAndExchange(
    expectedValue: Int,
    newValue: Int
): Int

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 Int: the expected value
newValue Int: the new value
Return
Int the witness value, which will be the same as the expected value if successful

compareAndExchangeAcquire

Added in API level 33
fun compareAndExchangeAcquire(
    expectedValue: Int,
    newValue: Int
): Int

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 Int: the expected value
newValue Int: the new value
Return
Int the witness value, which will be the same as the expected value if successful

compareAndExchangeRelease

Added in API level 33
fun compareAndExchangeRelease(
    expectedValue: Int,
    newValue: Int
): Int

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 Int: the expected value
newValue Int: the new value
Return
Int the witness value, which will be the same as the expected value if successful

compareAndSet

Added in API level 1
fun compareAndSet(
    expectedValue: Int,
    newValue: Int
): Boolean

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

Parameters
expectedValue Int: the expected value
newValue Int: 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(): Int

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

Equivalent to addAndGet(-1).

Return
Int the updated value

get

Added in API level 1
fun get(): Int

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

Return
Int the current value

getAcquire

Added in API level 33
fun getAcquire(): Int

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

Return
Int the value

getAndAccumulate

Added in API level 24
fun getAndAccumulate(
    x: Int,
    accumulatorFunction: IntBinaryOperator!
): Int

Atomically updates (with memory effects as specified by java.lang.invoke.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 Int: the update value
accumulatorFunction IntBinaryOperator!: a side-effect-free function of two arguments
Return
Int the previous value

getAndAdd

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

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

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

getAndDecrement

Added in API level 1
fun getAndDecrement(): Int

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

Equivalent to getAndAdd(-1).

Return
Int the previous value

getAndIncrement

Added in API level 1
fun getAndIncrement(): Int

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

Equivalent to getAndAdd(1).

Return
Int the previous value

getAndSet

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

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

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

getAndUpdate

Added in API level 24
fun getAndUpdate(updateFunction: IntUnaryOperator!): Int

Atomically updates (with memory effects as specified by java.lang.invoke.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 IntUnaryOperator!: a side-effect-free function
Return
Int the previous value

getOpaque

Added in API level 33
fun getOpaque(): Int

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

Return
Int the value

getPlain

Added in API level 33
fun getPlain(): Int

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

Return
Int the value

incrementAndGet

Added in API level 1
fun incrementAndGet(): Int

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

Equivalent to addAndGet(1).

Return
Int the updated value

lazySet

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

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

Parameters
newValue Int: the new value

set

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

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

Parameters
newValue Int: the new value

setOpaque

Added in API level 33
fun setOpaque(newValue: Int): Unit

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

Parameters
newValue Int: the new value

setPlain

Added in API level 33
fun setPlain(newValue: Int): Unit

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

Parameters
newValue Int: the new value

setRelease

Added in API level 33
fun setRelease(newValue: Int): Unit

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

Parameters
newValue Int: the new value

toDouble

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

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

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 current value of this AtomicInteger as a float after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

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 current value of this AtomicInteger as an int, with memory effects as specified by VarHandle#getVolatile. Equivalent to get().

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 current value of this AtomicInteger as a long after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

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: IntUnaryOperator!): Int

Atomically updates (with memory effects as specified by java.lang.invoke.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 IntUnaryOperator!: a side-effect-free function
Return
Int the updated value

weakCompareAndSet

Added in API level 1
Deprecated in API level 33
fun weakCompareAndSet(
    expectedValue: Int,
    newValue: Int
): Boolean

Deprecated: 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 Int: the expected value
newValue Int: the new value
Return
Boolean true if successful

weakCompareAndSetAcquire

Added in API level 33
fun weakCompareAndSetAcquire(
    expectedValue: Int,
    newValue: Int
): Boolean

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

Parameters
expectedValue Int: the expected value
newValue Int: the new value
Return
Boolean true if successful

weakCompareAndSetPlain

Added in API level 33
fun weakCompareAndSetPlain(
    expectedValue: Int,
    newValue: Int
): Boolean

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

Parameters
expectedValue Int: the expected value
newValue Int: the new value
Return
Boolean true if successful

weakCompareAndSetRelease

Added in API level 33
fun weakCompareAndSetRelease(
    expectedValue: Int,
    newValue: Int
): Boolean

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

Parameters
expectedValue Int: the expected value
newValue Int: the new value
Return
Boolean true if successful

weakCompareAndSetVolatile

Added in API level 33
fun weakCompareAndSetVolatile(
    expectedValue: Int,
    newValue: Int
): Boolean

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

Parameters
expectedValue Int: the expected value
newValue Int: the new value
Return
Boolean true if successful