简体   繁体   中英

Kotlin Coroutines: one single coroutine at a time in single thread

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

  • ...
  • the "async block #2" printed
  • task 2 is running on the singleThread
  • task 2 is suspended with delay(1500) , the singleThread is free
  • task 1 is started on the singleThread
  • task 1 is suspended with delay(500) , the singleThread is free
  • at that point we have the delay queue:
    • resume the delay(500) for task 1
    • resume the delay(1500) for task 2
  • after some time
  • resume(500) schedules the second part of task 1 to run in the singleThread
  • after some time
  • resume(1500) schedules the second part of task 2 to run in the singleThread

In 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM