compareAndExchange

Native
1.3
fun compareAndExchange(
    index: Int,
    expectedValue: Int,
    newValue: Int
): Int

(source)

Atomically sets the value of the element at the given index to the new value if the current value equals the expected value and returns the old value of the element in any case.

Provides sequential consistent ordering guarantees and never fails spuriously.

Exceptions

IndexOutOfBoundsException - if the index is out of bounds of this array.