Exercise: Experiments with dispatchers
Experiment with how dispatcher choice influences execution time when you start 100 coroutines, each of which performs the following operations:
val dispatcher = Dispatchers.IO.limitedParallelism(1)
//val dispatcher = Dispatchers.Default
//val dispatcher = Dispatchers.IO
//val dispatcher = Dispatchers.IO.limitedParallelism(100)
val operation = ::cpu1
//val operation = ::blocking
//val operation = ::suspending
fun cpu1() {
var i = Int.MAX_VALUE
while (i > 0) i -= if (i % 2 == 0) 1 else 2
}
fun blocking() {
Thread.sleep(1000)
}
suspend fun suspending() {
delay(1000)
}
Test the following dispatchers:
- Dispatcher limited to 1 thread
Dispatchers.Default
Dispatchers.IO
- Dispatcher limited to 100 threads
Test all combinations and fill the table below with the results. Then, try to explain the results.
Dispatcher | CPU-intensive | Blocking | Suspending |
---|
1 thread | | | |
Dispatchers.Default | | | |
Dispatchers.IO | | | |
100 threads | | | |
You can use the following code to measure execution time:
suspend fun main(): Unit = measureTimeMillis {
coroutineScope {
repeat(100) {
launch(dispatcher) {
operation()
println("Done $it")
}
}
}
}.let { println("Took $it") }
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in coroutines/dispatcher/Experiments.kt. You can find there starting code and example usage.
Once you are done with the exercise, you can check your solution here.
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.system.measureTimeMillis
val dispatcher = Dispatchers.IO.limitedParallelism(1)
//val dispatcher = Dispatchers.Default
//val dispatcher = Dispatchers.IO
//val dispatcher = Dispatchers.IO.limitedParallelism(100)
val operation = ::cpu1
//val operation = ::blocking
//val operation = ::suspending
fun cpu1() {
var i = Int.MAX_VALUE
while (i > 0) i -= if (i % 2 == 0) 1 else 2
}
fun blocking() {
Thread.sleep(1000)
}
suspend fun suspending() {
delay(1000)
}
suspend fun main(): Unit = measureTimeMillis {
coroutineScope {
repeat(100) {
launch(dispatcher) {
operation()
println("Done $it")
}
}
}
}.let { println("Took $it") }
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.