import kotlinx.coroutines.*
suspend fun greet(name: String) {
delay(50)
println("hello $name")
}
fun main() = runBlocking {
// Spawn 3 fire-and-forget coroutines
val jobs = listOf("Alice", "Bob", "Cara").map { name ->
launch { greet(name) }
}
// Wait for all of them
jobs.joinAll()
println("all done")
// Cancel a still-running job
val long = launch {
repeat(100) { i ->
delay(50)
println("tick $i")
}
}
delay(120)
long.cancel()
long.join()
println("cancelled")
}
Create a free account and build your private vault. Share publicly whenever you want.