File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew2220 Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ import kotlin.concurrent.atomics.*
2+ import kotlin.random.Random
3+
4+ @OptIn(ExperimentalAtomicApi ::class )
5+ fun main () {
6+ val counter = AtomicLong (Random .nextLong())
7+ val minSetBitsThreshold = 20
8+
9+ // Sets a new value without using the result
10+ counter.update { if (it < 0xDECAF ) 0xCACA0 else 0xC0FFEE }
11+
12+ // Retrieves the current value, then updates it
13+ val previousValue = counter.fetchAndUpdate { 0x1CEDL .shl(Long .SIZE_BITS - it.countLeadingZeroBits()) or it }
14+
15+ // Updates the value, then retrieves the result
16+ val current = counter.updateAndFetch {
17+ if (it.countOneBits() < minSetBitsThreshold) it.shl(20 ) or 0x15BADL else it
18+ }
19+
20+ val hexFormat = HexFormat {
21+ upperCase = true
22+ number {
23+ removeLeadingZeros = true
24+ }
25+ }
26+ println (" Previous value: ${previousValue.toHexString(hexFormat)} " )
27+ println (" Current value: ${current.toHexString(hexFormat)} " )
28+ println (" Expected status flag set: ${current and 0xBAD != 0xBADL } " )
29+ }
You can’t perform that action at this time.
0 commit comments