compareAndExchange

expect fun compareAndExchange(expectedValue: Boolean, newValue: Boolean): Boolean(source)

Atomically stores the given new value into this AtomicBoolean if the current value equals the expected value and returns the old value in any case.

Comparison of values is done by value.

Since Kotlin

2.1

Samples

import kotlin.concurrent.atomics.*
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val a = AtomicBoolean(true)
// Current value true is equal to the expected value true ->
// compareAndExchange succeeds, stores the new value false and returns the old value true.
println(a.compareAndExchange(true, false)) // true
println(a.load()) // false

// Current value false is not equal to the expected value true ->
// compareAndExchange fails, does not store the new value and returns the current value false.
println(a.compareAndExchange(true, false)) // false
println(a.load()) // false 
   //sampleEnd
}
actual fun compareAndExchange(expectedValue: Boolean, newValue: Boolean): Boolean(source)

Atomically stores the given new value into this AtomicBoolean if the current value equals the expected value and returns the old value in any case.

Comparison of values is done by value.

Since Kotlin

2.1
actual fun compareAndExchange(expectedValue: Boolean, newValue: Boolean): Boolean(source)

Atomically stores the given new value into this AtomicBoolean if the current value equals the expected value and returns the old value in any case.

Comparison of values is done by value.

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

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

Since Kotlin

2.1
actual fun compareAndExchange(expectedValue: Boolean, newValue: Boolean): Boolean(source)

Atomically stores the given new value into this AtomicBoolean if the current value equals the expected value and returns the old value in any case.

Comparison of values is done by value.

Since Kotlin

2.1
actual fun compareAndExchange(expectedValue: Boolean, newValue: Boolean): Boolean(source)

Atomically stores the given new value into this AtomicBoolean if the current value equals the expected value and returns the old value in any case.

Comparison of values is done by value.

Since Kotlin

2.1
actual fun compareAndExchange(expectedValue: Boolean, newValue: Boolean): Boolean(source)

Atomically stores the given new value into this AtomicBoolean if the current value equals the expected value and returns the old value in any case.

Comparison of values is done by value.

Since Kotlin

2.1