Hey I am learning atomic integer in kotlin. I want to know is there atomic integer swap value if current value is smaller than new value. For example
AtomicInt 10
S*cenario 1*
new value is 5 and it changes to 5 because 5 is lower than 10
Scenario 2 new value is 20 it will not change the atomic value because 20 is greater than 10.
Can we do this throught CompareAndSwap function?
a
Alexander Maryanovsky
02/20/2022, 5:14 AM
You can use
updateAndGet
to implement whatever logic you want.
e
ephemient
02/20/2022, 8:15 AM
or
.getAndAccumulate(newValue, ::minOf)
but either way, this is a Java API, not a Kotlin one
☝️ 1
v
Vivek Modi
02/20/2022, 10:21 AM
Thanks
e
ephemient
02/20/2022, 11:52 AM
FYI your link points to the kotlin.native AtomicInt, which does not support these operations as it is actually using atomic operations (instead of having some methods which use synchronization like Java's AtomicInteger), and is not available on Android