compareAndSetAt
Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value. Returns true if the operation was successful and false only if the current value of the element was not equal to the expected value.
This operation has so-called strong semantics, meaning that it returns false if and only if current and expected values are not equal.
Comparison of values is done by reference.
Since Kotlin
2.1Throws
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" -> compareAndSetAt succeeds.
println(a.compareAndSetAt(index = 1, expectedValue = "bbb", newValue = "kkk")) // true
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" -> compareAndSetAt fails.
println(a.compareAndSetAt(index = 2, expectedValue = "aaa", newValue = "jjj")) // false
println(a.loadAt(2)) // ccc
println(a.toString()) // [aaa, kkk, ccc]
//sampleEnd
}
Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value. Returns true if the operation was successful and false only if the current value of the element was not equal to the expected value.
Comparison of values is done by value.
Since Kotlin
2.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value. Returns true if the operation was successful and false only if the current value of the element was not equal to the expected value.
Comparison of values is done by value.
Has the same memory effects as java.util.concurrent.atomic.AtomicReferenceArray.compareAndSet.
Since Kotlin
2.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value. Returns true if the operation was successful and false only if the current value of the element was not equal to the expected value.
This operation has so-called strong semantics, meaning that it returns false if and only if current and expected values are not equal.
Comparison of values is done by reference.
Since Kotlin
2.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value. Returns true if the operation was successful and false only if the current value of the element was not equal to the expected value.
Comparison of values is done by value.
Since Kotlin
2.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicArray at the given index if the current value equals the expected value. Returns true if the operation was successful and false only if the current value of the element was not equal to the expected value.
Comparison of values is done by value.
Since Kotlin
2.1Throws
if the index is out of bounds of this array.