Coroutines:main()runblocking vs悬浮

发布于 2025-01-25 08:29:06 字数 687 浏览 1 评论 0原文

取以下两个代码示例(分别从Kotlin文档和Coroutines Library readme中获取):

import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(2000)
        println("World")
    }
    println("Hello")
}

在Kotlin> = 1.3.0中,一个人可以将main()函数标记为直接使用CoroutinesCope

import kotlinx.coroutines.*

suspend fun main() = coroutineScope {
    launch {
        delay(2000)
        println("World")
    }
    println("Hello")
}

两者都产生相同的输出:

Hello
World

两种方法之间是否存在任何功能差异?如果是,他们是什么?

Take the following two samples of code (taken from the Kotlin documentation and from the Coroutines library README respectively):

import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(2000)
        println("World")
    }
    println("Hello")
}

In Kotlin >= 1.3.0 one can mark the main() function as suspendable and use a coroutineScope directly.

import kotlinx.coroutines.*

suspend fun main() = coroutineScope {
    launch {
        delay(2000)
        println("World")
    }
    println("Hello")
}

Both produce the same output:

Hello
World

Are there any functional differences between the two approaches? If yes, what are they?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2025-02-01 08:29:06

runblocking()coroutinescope()在功能上非常相似。他们俩都暂停当前的执行等待,直到内部的Coroutine完成。不同之处在于,runblocking()是要从常规的非可疑代码执行,因此它通过阻止和coroutinescope()等待,从悬浮上下文中使用,因此它暂停。

这种差异使他们在内部做很多不同的事情。 Runblocking()必须先初始化整个Coroutines机械,然后才能执行可疑的lambda。 coroutinescope()有访问现有的coroutine上下文,因此它仅计划执行内部lambda并暂停。

现在,暂停Fun Main()只是Kotlin编译器提供的“快捷方式”,以使初始化Coroutines应用程序更容易。在内部,它在真实的主函数和您的悬浮主函数之间创建桥梁。它生成一个单独的main()函数,该功能不可暂停,是“真实”主函数。此功能初始化了一个coroutine,并使用内部runsuspend()实用程序来执行您的暂停fun main()。在此处进行描述: https://github.com/kotlin/kotllin/kob/master/proposals/enhancing-main-convention.md#implementation-details-on-jvm-1

启动Coroutine应用程序的两种方法都是非常非常类似,您可以根据自己的口味选择。一个值得注意的区别是runblocking()使用当前的“主”线程创建调度程序。 暂停Fun Main()还使用主线程执行,但并未指定调度程序,因此每当您使用EG coroutinescope()时,它将切换到> dispatchers.default。结果,您的runblocking()示例在您的coroutinescope()示例中使用单线读取器,示例使用多线程dispatchers.default.default。但是,在两种方法之间选择时,不应真正考虑这种差异。我们可以随时轻松地切换调度员。

runBlocking() and coroutineScope() are functionally very similar. They both pause the current execution waiting until the inner coroutine finishes. The difference is that runBlocking() is meant to be executed from a regular non-suspendable code, so it waits by blocking and coroutineScope() is used from suspendable context, so it suspends.

That difference makes them do much different things internally. runBlocking() has to first initialize the whole coroutines machinery before it could execute a suspendable lambda. coroutineScope() has access to existing coroutine context, so it only schedules the inner lambda to be executed and suspends.

Now, suspend fun main() is just a "shortcut" that Kotlin compiler provides to make it easier to initialize coroutines apps. Internally, it creates a bridge between the real main function and your suspendable main function. It generates a separate main() function, which is not suspendable and is the "real" main function. This function initializes a coroutine and uses internal runSuspend() utility to execute your suspend fun main(). This is described here: https://github.com/Kotlin/KEEP/blob/master/proposals/enhancing-main-convention.md#implementation-details-on-jvm-1

Both methods of starting a coroutine application are very similar and you can choose according to your taste. One notable difference is that runBlocking() creates a dispatcher using the current "main" thread. suspend fun main() also executes using the main thread, but it doesn't specify the dispatcher, so whenever you use e.g. coroutineScope() it will switch to Dispatchers.Default. As a result, your runBlocking() example uses single-threaded dispatcher while your coroutineScope() example uses multi-threaded Dispatchers.Default. However, this difference should not really be taken into account when choosing between both methods. We can very easily switch dispatchers at any time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文