Google is committed to advancing racial equity for Black communities. See how.
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 java.util.concurrent.atomic package specification for description of the properties of atomic variables. 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
<init>(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 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.

Boolean
compareAndSet(expect: Int, update: Int)

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

Int

Atomically decrements by one the current value.

Int
get()

Gets the current value.

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

Atomically updates 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.

Int

Atomically decrements by one the current value.

Int

Atomically increments by one the current value.

Int
getAndSet(newValue: Int)

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

Int
getAndUpdate(updateFunction: IntUnaryOperator!)

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

Int

Atomically increments by one the current value.

Unit
lazySet(newValue: Int)

Eventually sets to the given value.

Unit
set(newValue: Int)

Sets to the given value.

open Double

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

open Float

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

open Int

Returns the value of this AtomicInteger as an int.

open Long

Returns the value of this AtomicInteger as a long after a widening primitive conversion.

open String

Returns the String representation of the current value.

Int
updateAndGet(updateFunction: IntUnaryOperator!)

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

Boolean
weakCompareAndSet(expect: Int, update: Int)

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

Public constructors

<init>

Added in API level 1
AtomicInteger(initialValue: Int)

Creates a new AtomicInteger with the given initial value.

Parameters
initialValue Int: the initial value

<init>

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

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

compareAndSet

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

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

Parameters
expect Int: the expected value
update 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 by one the current value.

Return
Int the updated value

get

Added in API level 1
fun get(): Int

Gets the current value.

Return
Int the current value

getAndAccumulate

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

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

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

getAndDecrement

Added in API level 1
fun getAndDecrement(): Int

Atomically decrements by one the current value.

Return
Int the previous value

getAndIncrement

Added in API level 1
fun getAndIncrement(): Int

Atomically increments by one the current value.

Return
Int the previous value

getAndSet

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

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

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

getAndUpdate

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

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 IntUnaryOperator!: a side-effect-free function
Return
Int the previous value

incrementAndGet

Added in API level 1
fun incrementAndGet(): Int

Atomically increments by one the current value.

Return
Int the updated value

lazySet

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

Eventually sets to the given value.

Parameters
newValue Int: the new value

set

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

Sets to the given value.

Parameters
newValue Int: the new value

toDouble

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

Returns the value of this AtomicInteger 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 AtomicInteger 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 AtomicInteger as an int. 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 value of this AtomicInteger as a long after a widening primitive conversion.

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 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
fun weakCompareAndSet(
    expect: Int,
    update: Int
): 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 Int: the expected value
update Int: the new value
Return
Boolean true if successful