CoroutineScope背后的概念是什么? [英] Whats the concept behind a CoroutineScope?

查看:344
本文介绍了CoroutineScope背后的概念是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 CoroutineScope 我还是有点困惑CoroutineScope背后的想法是什么.

After reading the introduction and the javadoc of CoroutineScope I'm still a little confused what the idea behind a CoroutineScope is.

文档的第一句话为新协程定义范围".我不清楚:为什么我的协程需要一个瞄准镜?

The first sentence of the doc "Defines a scope for new coroutines." is not clear to me: Why do my coroutines need a scope?

此外,为什么不赞成使用独立的协程构建器?为什么这样做会更好:

Also, why are standalone coroutine builders deprecated? Why is it better to do this:

fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
    for (x in 1..5) send(x * x)
}

代替

fun produceSquares(): ReceiveChannel<Int> = produce { //no longer an extension function
    for (x in 1..5) send(x * x)
}

推荐答案

您仍然可以通过在GlobalScope中生成它们来使用全局独立"协程:

You can still use global "standalone" coroutines by spawning them in GlobalScope:

GlobalScope.launch {
    println("I'm running unstructured")
}

但是,不建议这样做,因为在全局范围内创建协程与使用良好的旧线程基本相同.您创建了它们,但是以某种方式需要跟踪引用以稍后加入/取消它们.

However, it's not recommended to do this since creating coroutines on a global scope is basically the same we did with good old threads. You create them but somehow need to keep track of a reference to later join/cancel them.

使用结构化并发,即将协程嵌套在它们的作用域中,您将在总体上拥有一个更易于维护的系统.例如,如果在另一个内部生成一个协程,则您将继承外部作用域.这具有多个优点.如果取消外部协程,则取消将委派给内部协程.另外,您可以确保在所有子协程完成工作之前,外部协程不会完成.

Using structured concurrency, that is nesting coroutines in their scopes, you will have a more maintainable system overall. For example, if you spawn a coroutine inside another one, you inherit the outer scope. This has multiple advantages. If you cancel the outer coroutine, the cancellation will be delegated to its inner coroutines. Also, you can be sure that the outer coroutine will not complete before all its children coroutines have done their work.

There's also a very good example shown in the documentation for CoroutineScope.

CoroutineScope应该在具有明确定义的生命周期的实体上实施,这些实体负责启动子协程.在Android上,此类实体的示例是Activity.

CoroutineScope should be implemented on entities with well-defined lifecycle that are responsible for launching children coroutines. Example of such entity on Android is Activity.

毕竟,显示的produceSquares方法的第一个版本更好,因为只有在CoroutineScope中调用时,它才是可执行的.这意味着您可以在其他任何协程中运行它:

After all, the first version of your shown produceSquares methods is better as it is only executable if invoked in a CoroutineScope. That means you can run it inside any other coroutine:

launch {
    produceSquares()
}

produceSquares内部创建的协程继承了launch的范围.您可以确定launchproduceSquares之前没有完成.另外,如果您取消了launch,这也会影响produceSquares.

The coroutine created inside produceSquares inherits the scope of launch. You can be sure that launch does not complete before produceSquares. Also, if you cancelled launch, this would also effect produceSquares.

此外,您仍然可以像下面这样创建全局运行的协程:

Furthermore, you can still create a globally running coroutine like this:

GlobalScope.produceSquares()

但是,如上所述,在大多数情况下,这并不是最佳选择.

But, as mentioned, that's not the best option in most cases.

我也想推广我写的一篇文章.有一些示例说明了范围的含义: https://kotlinexpertise.com/kotlin-coroutines-concurrency/

I'd also like to promote an article I wrote. There are some examples demonstrating what scopes mean: https://kotlinexpertise.com/kotlin-coroutines-concurrency/

这篇关于CoroutineScope背后的概念是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆