compareAndExchangeAt
Atomically stores the new value into the element of this AtomicLongArray 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.1Throws
if the index is out of bounds of this array.
Samples
import kotlin.concurrent.atomics.*
fun main() {
//sampleStart
val a = AtomicLongArray(longArrayOf(1, 2, 3))
// Current value of a[1] is 2, it is equal to the expected value 2 ->
// compareAndExchangeAt succeeds, stores the new value 7 to a[1] and returns the old value 2.
println(a.compareAndExchangeAt(index = 1, expectedValue = 2, newValue = 7)) // 2
println(a.loadAt(1)) // 7
println(a.toString()) // [1, 7, 3]
// Current value of a[2] is 3, it is not equal to the expected value 4 ->
// compareAndExchangeAt fails, does not store the new value to a[2] and returns the current value 3.
println(a.compareAndExchangeAt(index = 2, expectedValue = 4, newValue = 25)) // 3
println(a.loadAt(2)) // 3
println(a.toString()) // [1, 7, 3]
//sampleEnd
}
Atomically stores the new value into the element of this AtomicLongArray 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.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicLongArray 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.AtomicLongArray.compareAndSet, since java.util.concurrent.atomic.AtomicLongArray.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.AtomicLongArray.compareAndExchange for users running JDK 9 or higher.
Since Kotlin
2.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicLongArray 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.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicLongArray 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.1Throws
if the index is out of bounds of this array.
Atomically stores the new value into the element of this AtomicLongArray 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.1Throws
if the index is out of bounds of this array.