```suspend fun service(): String = coroutineScope ...
# coroutines
u
Copy code
suspend fun service(): String = coroutineScope {
    launch(Dispatchers.Default) { getOrder() }
    launch(<http://Dispatchers.IO|Dispatchers.IO>) { getProduct() }
    println("this = ${coroutineContext.job}")
    "service"
}

suspend fun getOrder(): String {
    <http://logger.info|logger.info> { "get ordering!!! [Order Thread(${Thread.currentThread().id}) : ${Thread.currentThread().name}]" }
    var no = 10
    while (true) {
        if (no == 20) break
        no++
        <http://logger.info|logger.info> { "$no [Order Thread(${Thread.currentThread().id}) : ${Thread.currentThread().name}]" }
        delay(700)
    }
    return "order"
}

suspend fun getProduct(): String {
    <http://logger.info|logger.info> { "get product!!! [Product Thread(${Thread.currentThread().id}) : ${Thread.currentThread().name}]" }
    var no = 10
    while (true) {
        if (no == 20) break
        no++
        <http://logger.info|logger.info> { "$no [Product Thread(${Thread.currentThread().id}) : ${Thread.currentThread().name}]" }
        delay(300)
    }
    return "product"
}

private val logger = KotlinLogging.logger {}
I have a question about running coroutines by Dispatchers. On this code, i launched two suspendable functions at service function, getOrder func launched by Dispatchers.Default but the next one getProduct is by Dispatchers.IO. But when this simple program finished, the log message like that. All coroutines has launched by
Dispatchers.Default.
Why coroutines that resuming getProduct functions has running on Dispatchers.Default not Dispatchers.IO?
Copy code
17:17:19.713 [DefaultDispatcher-worker-1 @coroutine#1] INFO  m.y.h.study.Study4 - get ordering!!! [Order Thread(14) : DefaultDispatcher-worker-1 @coroutine#1]
17:17:19.713 [DefaultDispatcher-worker-2 @coroutine#2] INFO  m.y.h.study.Study4 - get product!!! [Product Thread(15) : DefaultDispatcher-worker-2 @coroutine#2]
17:17:19.714 [DefaultDispatcher-worker-1 @coroutine#1] INFO  m.y.h.study.Study4 - 11 [Order Thread(14) : DefaultDispatcher-worker-1 @coroutine#1]
17:17:19.714 [DefaultDispatcher-worker-2 @coroutine#2] INFO  m.y.h.study.Study4 - 11 [Product Thread(15) : DefaultDispatcher-worker-2 @coroutine#2]
17:17:20.019 [DefaultDispatcher-worker-1 @coroutine#2] INFO  m.y.h.study.Study4 - 12 [Product Thread(14) : DefaultDispatcher-worker-1 @coroutine#2]
...
s
The IO dispatcher actually shares threads with the default dispatcher. It’s so that you can switch from one dispatcher to the other without incurring the cost of a thread switch.
👀 1
There is a note in the docs that mentions it. Basically there is one large pool of threads shared by both dispatchers. The default dispatcher reserves a certain number of those threads for non-blocking work.
👀 1
You can log the value of
coroutineContext[CoroutineDispatcher]
instead of the thread name if you want to see the actual dispatcher.
thank you color 2
u
Ok io dispatcher shares default dispatcher’s threads basically. If all of shared threads are busy, then io dispatcher can create new thread and destroy it when does not neseccary?
s
👍 1
🙇 1
u
Thank you for your help!
🍻 1
s
Here's a great read on Dispatchers from Marcin Moskała, it also explains relations between Dispatchers.IO and Dispatchers.Default: https://kt.academy/article/cc-dispatchers
👀 1