compare And Exchange At
Платформа и требования к версии: Common (2.1), JS (2.1), Native (2.1), Wasm-JS (2.1), Wasm-WASI (2.1)
expect fun compareAndExchangeAt(index: Int, expectedValue: Int, newValue: Int): Int
Атомарно записывает новое значение в элемент этого массива AtomicIntArray по указанному индексу, если текущее значение равно ожидаемому значению, и в любом случае возвращает старое значение элемента.
Сравнение значений выполняется по значению.
Исключения
если индекс выходит за границы этого массива.
Примеры
import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
val a = AtomicIntArray(intArrayOf(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
}
Платформа и требования к версии: JVM (2.1)
actual fun compareAndExchangeAt(index: Int, expectedValue: Int, newValue: Int): Int
Атомарно записывает новое значение в элемент этого массива AtomicIntArray по указанному индексу, если текущее значение равно ожидаемому значению, и в любом случае возвращает старое значение элемента.
Сравнение значений выполняется по значению.
Для обеспечения совместимости с Java 8 compareAndExchangeAt реализован с использованием java.util.concurrent.atomic.AtomicIntegerArray.compareAndSet, поскольку метод java.util.concurrent.atomic.AtomicIntegerArray.compareAndExchange доступен только начиная с Java 9.
В будущих выпусках планируется делегировать реализацию compareAndExchangeAt методу java.util.concurrent.atomic.AtomicIntegerArray.compareAndExchange для пользователей, работающих на JDK 9 или выше.
Исключения
если индекс выходит за границы этого массива.
Примеры
import kotlin.concurrent.atomics.*
import kotlin.concurrent.atomics.AtomicArray
import kotlin.test.assertFailsWith
fun main() {
//sampleStart
val a = AtomicIntArray(intArrayOf(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
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.concurrent.atomics/-atomic-int-array/compare-and-exchange-at.html