Hey I am learning atomic integer in kotlin. I want...
# android
v
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
You can use
updateAndGet
to implement whatever logic you want.
e
or
.getAndAccumulate(newValue, ::minOf)
but either way, this is a Java API, not a Kotlin one
☝️ 1
v
Thanks
e
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
v
Thanks got it