Consider this code below, I'm trying to use Executors.newFixedThreadPool(1).asCoroutineDispatcher() to create a single thread dispatcher; I want code within launch(singleThread){...} to be executed sequentially
expected result should be like below because async-block#2 reach/acquire singleThread first
async block #2
async block #1
single thread block #2
single thread block #1
The answer is 3
but the actual result is
async block #2
async block #1
single thread block #1
single thread block #2
The answer is 3
single-thread-block-#2 and single-thread-block-#1 seem to run in parallel, singleThread makes no different here
import java.util.concurrent.Executors
import kotlinx.coroutines.*
import kotlin.system.*
val singleThread = Executors.newFixedThreadPool(1).asCoroutineDispatcher()
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = async { // async block #1
delay(200)
println("async block #1")
launch (singleThread) {
delay(500)
println("single thread block #1")
}
2
}
val two = async { // async block #2
delay(100)
println("async block #2")
launch (singleThread) {
delay(1500)
println("single thread block #2")
}
1
}
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
}
Mind the delay() is suspend function in the code. It is implemented via coroutine suspension. It means that the execution of the code is suspended at the moment when you call the delay and only resumed after the timeout. The thread (for example one that you use via async(singleThread) {..} is not busy waiting to the time to elapse.
The overall scenario looks like that
singleThreaddelay(1500), the singleThread is freesingleThreaddelay(500), the singleThread is freedelay(500) for task 1delay(1500) for task 2resume(500) schedules the second part of task 1 to run in the singleThreadresume(1500) schedules the second part of task 2 to run in the singleThreadIn addition to the @EugenePetrenko answer the is a new method CoroutineDispatcher.limitedParallelism(numberOfParallelism), which you can use to guarantees the parallelism restriction - at most 1 coroutine can be executed concurrently in this dispatcher. It will look like:
val singleThread = Dispatchers.IO.limitedParallelism(1)
someCoroutineScope.launch (singleThread) {
...
}
Function limitedParallelism is available starting from the 1.6.0 version of the kotlinx.coroutines library.