Solution: Dispatchers
Explore how Kotlin coroutine dispatchers operate and learn to construct coroutine scopes to manage concurrency. This lesson guides you through practical examples detailing dispatcher usage, parallelism limits, and coroutine execution flow.
We'll cover the following...
We'll cover the following...
Solution
The solution to the challenge we just solved is as follows.
var i = 0
suspend fun main(): Unit = coroutineScope {
val dispatcher = Dispatchers.Default
.limitedParallelism(1)
repeat(10000) {
launch(dispatcher) {
i++
}
}
delay(2000)
println(i)
}Solution to the challenge
Explanation
Here is a line–by–line explanation of the code above:
-
...