AtomicInt

An Int value that may be updated atomically.

Platform-specific implementation details:

When targeting the Native backend, AtomicInt stores a volatile Int variable and atomically updates it. For additional details about atomicity guarantees for reads and writes see kotlin.concurrent.Volatile.

When targeting the JVM, instances of AtomicInt are represented by java.util.concurrent.atomic.AtomicInteger. For details about guarantees of volatile accesses and updates of atomics refer to The Java Language Specification (17.4 Memory Model).

For JS and Wasm AtomicInt is implemented trivially and is not thread-safe since these platforms do not support multi-threading.

Since Kotlin

2.1

An Int value that may be updated atomically.

Since the JS platform does not support multi-threading, the implementation is trivial and has no atomic synchronizations.

Since Kotlin

2.1

An Int value that may be updated atomically.

Instances of AtomicInt are represented by java.util.concurrent.atomic.AtomicInteger and provide the the same atomicity guarantees.

Since Kotlin

2.1

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   // The atomic counter of processed items
val processedItems = AtomicInt(0)
val totalItems = 100
val items = List(totalItems) { "item$it" }
// Split the items into chunks for processing by multiple threads
val chunkSize = 20
val itemChunks = items.chunked(chunkSize)
val threads = itemChunks.map { chunk ->
    thread {
        for (item in chunk) {
            println("Processing $item in thread ${Thread.currentThread()}")
            // Increment the counter atomically
            processedItems += 1
        }
    }
}
threads.forEach { it.join() }
// The total number of processed items
println(processedItems.load()) // totalItems.toString() 
   //sampleEnd
}

An Int value that may be updated atomically.

Read operation load has the same memory effects as reading a Volatile property; Write operation store has the same memory effects as writing a Volatile property; Read-modify-write operations, like exchange, compareAndSet, compareAndExchange, fetchAndAdd, addAndFetch, have the same memory effects as reading and writing a Volatile property.

For additional details about atomicity guarantees for reads and writes see kotlin.concurrent.Volatile.

Since Kotlin

2.1

An Int value that may be updated atomically.

Since the Wasm platform does not support multi-threading, the implementation is trivial and has no atomic synchronizations.

Since Kotlin

2.1

An Int value that may be updated atomically.

Since the Wasm platform does not support multi-threading, the implementation is trivial and has no atomic synchronizations.

Since Kotlin

2.1

Constructors

Link copied to clipboard
expect constructor(value: Int)
actual constructor(value: Int)
constructor(value: Int)
actual constructor(value: Int)
actual constructor(value: Int)
actual constructor(value: Int)

Properties

Link copied to clipboard
Since Kotlin 2.1

Functions

Link copied to clipboard
expect fun addAndFetch(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the new value.

Since Kotlin 2.1
actual fun addAndFetch(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the new value.

Since Kotlin 2.1
actual fun addAndFetch(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the new value.

Since Kotlin 2.1
actual fun addAndFetch(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the new value.

Since Kotlin 2.1
actual fun addAndFetch(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the new value.

Since Kotlin 2.1
actual fun addAndFetch(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the new value.

Since Kotlin 2.1
Link copied to clipboard
fun addAndGet(delta: Int): Int

Atomically adds the given value to the current value and returns the new value.

Since Kotlin 2.1
Link copied to clipboard
Link copied to clipboard
expect fun compareAndExchange(expectedValue: Int, newValue: Int): Int

Atomically stores the given new value into this AtomicInt if the current value equals the expected value and returns the old value in any case.

Since Kotlin 2.1
actual fun compareAndExchange(expectedValue: Int, newValue: Int): Int

Atomically stores the given new value into this AtomicInt. if the current value equals the expected value and returns the old value in any case.

Since Kotlin 2.1
actual fun compareAndExchange(expectedValue: Int, newValue: Int): Int

Atomically stores the given new value into this AtomicInt if the current value equals the expected value and returns the old value in any case.

Since Kotlin 2.1
actual fun compareAndExchange(expectedValue: Int, newValue: Int): Int

Atomically stores the given new value into this AtomicInt if the current value equals the expected value and returns the old value in any case.

Since Kotlin 2.1
actual fun compareAndExchange(expectedValue: Int, newValue: Int): Int

Atomically stores the given new value into this AtomicInt if the current value equals the expected value and returns the old value in any case.

Since Kotlin 2.1
actual fun compareAndExchange(expectedValue: Int, newValue: Int): Int

Atomically stores the given new value into this AtomicInt if the current value equals the expected value and returns the old value in any case.

Since Kotlin 2.1
Link copied to clipboard
expect fun compareAndSet(expectedValue: Int, newValue: Int): Boolean

Atomically stores the given new value into this AtomicInt if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Since Kotlin 2.1
actual fun compareAndSet(expectedValue: Int, newValue: Int): Boolean

Atomically stores the given new value into this AtomicInt. if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Since Kotlin 2.1
actual fun compareAndSet(expectedValue: Int, newValue: Int): Boolean

Atomically stores the given new value into this AtomicInt if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Since Kotlin 2.1
actual fun compareAndSet(expectedValue: Int, newValue: Int): Boolean

Atomically stores the given new value into this AtomicInt if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Since Kotlin 2.1
actual fun compareAndSet(expectedValue: Int, newValue: Int): Boolean

Atomically stores the given new value into this AtomicInt if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Since Kotlin 2.1
actual fun compareAndSet(expectedValue: Int, newValue: Int): Boolean

Atomically stores the given new value into this AtomicInt if the current value equals the expected value, returns true if the operation was successful and false only if the current value was not equal to the expected value.

Since Kotlin 2.1
Link copied to clipboard

Atomically decrements the current value of this AtomicInt by one and returns the new value.

Since Kotlin 2.1
Link copied to clipboard

Atomically decrements the current value by one and returns the new value.

Since Kotlin 2.1
Link copied to clipboard
expect fun exchange(newValue: Int): Int

Atomically stores the given new value into this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun exchange(newValue: Int): Int

Atomically stores the given new value into this AtomicInt. and returns the old value.

Since Kotlin 2.1
actual fun exchange(newValue: Int): Int

Atomically stores the given new value into this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun exchange(newValue: Int): Int

Atomically stores the new value into this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun exchange(newValue: Int): Int

Atomically stores the given new value into this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun exchange(newValue: Int): Int

Atomically stores the given new value into this AtomicInt and returns the old value.

Since Kotlin 2.1
Link copied to clipboard
expect fun fetchAndAdd(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun fetchAndAdd(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun fetchAndAdd(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun fetchAndAdd(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun fetchAndAdd(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the old value.

Since Kotlin 2.1
actual fun fetchAndAdd(delta: Int): Int

Atomically adds the given value to the current value of this AtomicInt and returns the old value.

Since Kotlin 2.1
Link copied to clipboard

Atomically decrements the current value of this AtomicInt by one and returns the old value.

Since Kotlin 2.1
Link copied to clipboard

Atomically increments the current value of this AtomicInt by one and returns the old value.

Since Kotlin 2.1
Link copied to clipboard
fun getAndAdd(delta: Int): Int

Atomically adds the given value to the current value and returns the old value.

Since Kotlin 2.1
Link copied to clipboard

Atomically decrements the current value by one and returns the old value.

Since Kotlin 2.1
Link copied to clipboard

Atomically increments the current value by one and returns the old value.

Since Kotlin 2.1
Link copied to clipboard
fun getAndSet(newValue: Int): Int

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

Since Kotlin 2.1
Link copied to clipboard

Atomically increments the current value of this AtomicInt by one and returns the new value.

Since Kotlin 2.1
Link copied to clipboard

Atomically increments the current value by one and returns the new value.

Since Kotlin 2.1
Link copied to clipboard
expect fun load(): Int

Atomically loads the value from this AtomicInt.

Since Kotlin 2.1
actual fun load(): Int

Atomically loads the value from this AtomicInt.

Since Kotlin 2.1
actual fun load(): Int

Atomically loads the value from this AtomicInt.

Since Kotlin 2.1
actual fun load(): Int

Atomically loads the value from this AtomicInt.

Since Kotlin 2.1
actual fun load(): Int

Atomically loads the value from this AtomicInt.

Since Kotlin 2.1
actual fun load(): Int

Atomically loads the value from this AtomicInt.

Since Kotlin 2.1
Link copied to clipboard

Atomically subtracts the given value from the current value of this AtomicInt.

Since Kotlin 2.1
Link copied to clipboard

Atomically adds the given value to the current value of this AtomicInt.

Since Kotlin 2.1
Link copied to clipboard
expect fun store(newValue: Int)

Atomically stores the new value into this AtomicInt.

Since Kotlin 2.1
actual fun store(newValue: Int)

Atomically stores the new value into this AtomicInt.

Since Kotlin 2.1
actual fun store(newValue: Int)

Atomically stores the new value into this AtomicInt.

Since Kotlin 2.1
actual fun store(newValue: Int)

Atomically stores the new value into this AtomicInt.

Since Kotlin 2.1
actual fun store(newValue: Int)

Atomically stores the new value into this AtomicInt.

Since Kotlin 2.1
actual fun store(newValue: Int)

Atomically stores the new value into this AtomicInt.

Since Kotlin 2.1
Link copied to clipboard
expect open override fun toString(): String

Returns the string representation of the underlying Int value.

Since Kotlin 2.1
actual open override fun toString(): String

Returns the string representation of the underlying Int value.

Since Kotlin 2.1
actual open override fun toString(): String

Returns the string representation of the underlying Int value.

Since Kotlin 2.1
actual open override fun toString(): String

Returns the string representation of the Int value stored in this AtomicInt.

Since Kotlin 2.1
actual open override fun toString(): String

Returns the string representation of the underlying Int value.

Since Kotlin 2.1
actual open override fun toString(): String

Returns the string representation of the underlying Int value.

Since Kotlin 2.1