Skip to content

Commit 2fd530d

Browse files
kotlin-samples-pusher-botdkrasnoff
authored andcommitted
test(samples): add new samples
1 parent 3b05bf5 commit 2fd530d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)