compareAndExchangeAt

expect fun compareAndExchangeAt(index: Int, expectedValue: T, newValue: T): T(source)

Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value and returns the old value of the element in any case.

Comparison of values is done by reference.

Since Kotlin

2.1

Throws

if the index is out of bounds of this array.

Samples

import kotlin.concurrent.atomics.*

fun main() { 
   //sampleStart 
   val a = AtomicArray(arrayOf("aaa", "bbb", "ccc"))
// Current value of a[1] is "bbb, it is equal to the expected value "bbb" ->
// compareAndExchangeAt succeeds, stores the new value "kkk" to a[1] and returns the old value "bbb".
println(a.compareAndExchangeAt(index = 1, expectedValue = "bbb", newValue = "kkk")) // bbb
println(a.loadAt(1)) // kkk
println(a.toString()) // [aaa, kkk, ccc]

// Current value of a[2] is "ccc", it is not equal to the expected value "aaa" ->
// compareAndExchangeAt fails, does not store the new value to a[2] and returns the current value "ccc".
println(a.compareAndExchangeAt(index = 2, expectedValue = "aaa", newValue = "jjj")) // ccc
println(a.loadAt(2)) // ccc
println(a.toString()) // [aaa, kkk, ccc] 
   //sampleEnd
}
actual fun compareAndExchangeAt(index: Int, expectedValue: T, newValue: T): T(source)

Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value and returns the old value of the element in any case.

Comparison of values is done by value.

Since Kotlin

2.1

Throws

if the index is out of bounds of this array.

actual fun compareAndExchangeAt(index: Int, expectedValue: T, newValue: T): T(source)

Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value and returns the old value of the element in any case.

Comparison of values is done by value.

In order to maintain compatibility with Java 8, compareAndExchangeAt is implemented using java.util.concurrent.atomic.AtomicReferenceArray.compareAndSet, since java.util.concurrent.atomic.AtomicReferenceArray.compareAndExchange method is only available starting from Java 9.

In the future releases it's planned to delegate the implementation of compareAndExchangeAt to java.util.concurrent.atomic.AtomicReferenceArray.compareAndExchange for users running JDK 9 or higher.

Since Kotlin

2.1

Throws

if the index is out of bounds of this array.

actual fun compareAndExchangeAt(index: Int, expectedValue: T, newValue: T): T(source)

Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value and returns the old value of the element in any case.

Comparison of values is done by reference.

Since Kotlin

2.1

Throws

if the index is out of bounds of this array.

actual fun compareAndExchangeAt(index: Int, expectedValue: T, newValue: T): T(source)

Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value and returns the old value of the element in any case.

Comparison of values is done by value.

Since Kotlin

2.1

Throws

if the index is out of bounds of this array.

actual fun compareAndExchangeAt(index: Int, expectedValue: T, newValue: T): T(source)

Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value and returns the old value of the element in any case.

Comparison of values is done by value.

Since Kotlin

2.1

Throws

if the index is out of bounds of this array.