@OptIn(ExperimentalAtomicApi::class)
suspend fun testExperimentalAtomicApi() {
var processedItems = AtomicInt(0)
val totalItems = 100
val items = List(totalItems) { "item$it" }
// Splits the items into chunks for processing by multiple coroutines
val chunkSize = 20
val itemChunks = items.chunked(chunkSize)// chunked函数 将List分割为指定大小的子数组(List),
// itemChunks 5个数组,每个数组里有20个元素
coroutineScope {
for (chunk in itemChunks) {
launch {
for (item in chunk) {
println("Processing $item in thread ${Thread.currentThread()}")
processedItems.incrementAndFetch()
}
}
}
}
println("processedItems = $processedItems")
}
suspend fun main() {
testExperimentalAtomicApi()
}
@OptIn(ExperimentalUuidApi::class)
fun testExperimentalUuidApi(){
// parse() accepts a UUID in a plain hexadecimal format
val uuid = Uuid.parse("550e8400e29b41d4a716446655440000")
// Converts it to the hex-and-dash format
val hexDashFormat = uuid.toHexDashString()
// Outputs the UUID in the hex-and-dash format
println(hexDashFormat)
// 550e8400-e29b-41d4-a716-446655440000
// Outputs UUIDs in ascending order
println(
listOf(
uuid,
Uuid.parse("780e8400e29b41d4a716446655440005"),
Uuid.parse("5ab88400e29b41d4a716446655440076")
).sorted()
)
// [550e8400-e29b-41d4-a716-446655440000, 5ab88400-e29b-41d4-a716-446655440076, 780e8400-e29b-41d4-a716-446655440005]
}
fun main() {
testExperimentalUuidApi()
}
@OptIn(ExperimentalTime::class)
fun testTimeClock() {
// Get the current moment in time
val currentInstant = Clock.System.now()
println("Current time: $currentInstant")
// Find the difference between two moments in time
val pastInstant = Instant.parse("2023-01-01T00:00:00Z")
val duration = currentInstant - pastInstant
println("Time elapsed since 2023-01-01: $duration")
}
fun main() {
testTimeClock()
}