Kotlin协程:挂起函数在哪个线程上运行?

huangapple go评论102阅读模式
英文:

Kotlin Coroutines: on which thread the suspend function runs on?

问题

以下是您要翻译的内容:

"I am new to Kotlin Coroutines and I have a question that I am not able to find a direct answer anywhere. The question is simple: if I have a coroutine and I call a suspend function (which could potentially call another one), from my understanding, the coroutine is suspended and the suspend function does not know about the coroutine anymore; then, within the suspend function, on what thread it runs if it still needs to process something (outside of another inner suspend function)?"

"Here are my experimental codes:

import kotlinx.coroutines.*

fun main(): Unit = runBlocking {
    println("parent ${Thread.currentThread().name}")
    test()

    launch (Dispatchers.IO) {
        println("parent2 ${Thread.currentThread().name}")
        test2()
    }
}

suspend fun test() {
    delay(1)
    println("child ${Thread.currentThread().name}") //Who processes this?
}

suspend fun test2() {
    delay(1)
    println("child2 ${Thread.currentThread().name}") //Who processes this?
}

It seems the within the suspend function, it is running on the same thread as the calling coroutine's. But I am not sure."

英文:

I am new to Kotlin Coroutines and I have a question that I am not able to find a direct answer anywhere. The question is simple: if I have a coroutine and I call a suspend function (which could potentially call another one), from my understanding, the coroutine is suspended and the suspend function does not know about the coroutine anymore; then, within the suspend function, on what thread it runs if it still needs to process something (outside of another inner suspend function)?

Here are my experimental codes:

import kotlinx.coroutines.*

fun main(): Unit = runBlocking {
    println("parent ${Thread.currentThread().name}")
    test()

    launch (Dispatchers.IO) {
        println("parent2 ${Thread.currentThread().name}")
        test2()
    }
}

suspend fun test() {
    delay(1)
    println("child ${Thread.currentThread().name}") //Who processes this?
}

suspend fun test2() {
    delay(1)
    println("child2 ${Thread.currentThread().name}") //Who processes this?
}

It seems the within the suspend function, it is running on the same thread as the calling coroutine's. But I am not sure.

答案1

得分: 2

它在调用它的同一线程上运行,直到第一个暂停点(调用另一个暂停函数以暂停为止),假定它调用了一个暂停函数。然后,当它从暂停中恢复时,它将在由当前 CoroutineContext 的调度程序分配的任何线程上运行。由调度程序来决定从自己的线程池中使用/重用线程的逻辑。

请注意,runBlocking 的 CoroutineContext 带有自己的单线程调度程序,它使用调用它的线程。

英文:

It runs on the same thread that called it, up to the first suspension point (call to another suspend function that suspends), assuming it calls a suspend function. Then when it resumes from suspension, it runs on whatever thread is assigned by the current CoroutineContext’s dispatcher. It’s up to the dispatcher to determine the logic of using/reusing threads from its own pool.

Note that runBlocking’s CoroutineContext comes with its own single-threaded dispatcher that uses the thread it was called from.

huangapple
  • 本文由 发表于 2023年6月25日 22:56:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定