Kotlin中CoroutineScope和coroutineScope的区别

Difference between CoroutineScope and coroutineScope in Kotlin

谁能说清楚函数 CoroutineScope()coroutineScope() 之间的关系?

当我尝试查看源代码时,我发现它们都是CoroutineScope.kt的函数。此外,coroutineScope()suspend 函数,而另一个是 normal 函数

以下是我能找到的文档:

/**
 * Creates a [CoroutineScope] that wraps the given coroutine [context].
 *
 * If the given [context] does not contain a [Job] element, then a default `Job()` is created.
 * This way, cancellation or failure or any child coroutine in this scope cancels all the other children,
 * just like inside [coroutineScope] block.
 */
@Suppress("FunctionName")
public fun CoroutineScope(context: CoroutineContext): CoroutineScope =
    ContextScope(if (context[Job] != null) context else context + Job())

/**
 * Creates a [CoroutineScope] and calls the specified suspend block with this scope.
 * The provided scope inherits its [coroutineContext][CoroutineScope.coroutineContext] from the outer scope, but overrides
 * the context's [Job].
 *
 * This function is designed for _parallel decomposition_ of work. When any child coroutine in this scope fails,
 * this scope fails and all the rest of the children are cancelled (for a different behavior see [supervisorScope]).
 * This function returns as soon as the given block and all its children coroutines are completed.
 * A usage example of a scope looks like this:
 *
 * ```
 * suspend fun showSomeData() = coroutineScope {
 *
 *   val data = async(Dispatchers.IO) { // <- extension on current scope
 *      ... load some UI data for the Main thread ...
 *   }
 *
 *   withContext(Dispatchers.Main) {
 *     doSomeWork()
 *     val result = data.await()
 *     display(result)
 *   }
 * }
 * ```
 *
 * The scope in this example has the following semantics:
 * 1) `showSomeData` returns as soon as the data is loaded and displayed in the UI.
 * 2) If `doSomeWork` throws an exception, then the `async` task is cancelled and `showSomeData` rethrows that exception.
 * 3) If the outer scope of `showSomeData` is cancelled, both started `async` and `withContext` blocks are cancelled.
 * 4) If the `async` block fails, `withContext` will be cancelled.
 *
 * The method may throw a [CancellationException] if the current job was cancelled externally
 * or may throw a corresponding unhandled [Throwable] if there is any unhandled exception in this scope
 * (for example, from a crashed coroutine that was started with [launch][CoroutineScope.launch] in this scope).
 */
public suspend fun <R> coroutineScope(block: suspend CoroutineScope.() -> R): R =
    suspendCoroutineUninterceptedOrReturn { uCont ->
        val coroutine = ScopeCoroutine(uCont.context, uCont)
        coroutine.startUndispatchedOrReturn(coroutine, block)
    }

我想弄清楚它们之间的区别。如果有人能回答什么时候使用哪一个,那将会很有帮助。

CoroutineScope() 是将 Context 作为输入并为您提供 ContextJob 作为 CoroutineScope 接口对象的方法。

您可以使用此对象启动协程作业,如下所示:

suspend fun doNotDoThis() {
  CoroutineScope(coroutineContext).launch {
      println("I'm confused")
  }
}

coroutineScope()block/labmda 作为协程执行 job:

   fun main() = runBlocking { // this: CoroutineScope
    launch { 
        delay(200L)
        println("Task from runBlocking")
    }

    coroutineScope { // Creates a new coroutine scope
        launch {
            delay(500L) 
            println("Task from nested launch")
        }

        delay(100L)
        println("Task from coroutine scope") // This line will be printed before nested launch
    }

    println("Coroutine scope is over") // This line is not printed until nested launch completes
}

参考文献:

Article-1

Article-2

Kotlin-Docs

我希望这能回答你的问题。

CoroutineScope() 只不过是 CoroutineScope 对象的工厂,而 CoroutineScope 对象不过是 CoroutineContext 的持有者。它在协程中没有积极的作用,但它是基础设施的重要组成部分,可以很容易地正确地进行结构化并发。这是因为 launchasync 等所有协程构建器都是 CoroutineScope 上的扩展函数并继承其上下文。

你很少,如果有的话,需要调用 CoroutineScope() 因为通常你要么选择一个现有的协程作用域,要么让其他便利函数为你创建一个(比如 MainScope Android) 或 Kotlin 内部结构。

另一方面,

coroutineScope() 是一个函数,它执行您在子协程中传递给它的块。它基本上是 withContext(this.coroutineContext) 的一个别名,当你想在前台继续一些工作的同时启动一个或多个后台协程时,你应该主要使用它,然后在完成块时加入后台协程。

CoroutineScope(大写 C 版本)与 coroutineScope(较小的 C 版本)之间的最大区别,我可以弄清楚并且很容易理解的是将它们与 Unstructured 结构化 并发性

让我举个例子:

class MainActivity : AppCompatActivity() {
    private lateinit var btn: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn = findViewById(R.id.start_btn)
        btn.setOnClickListener {
            CoroutineScope(Dispatchers.Main).launch {
                val result = downloadUserData()
                Toast.makeText(applicationContext, "Result : $result", Toast.LENGTH_LONG).show()
            }
        }
    }

    private suspend fun downloadUserData(): Int {
        var result = 0
        // Here, we use CoroutineScope (Capital C version) which will start a new scope and
        // launch coroutine in new scope Dispatchers.IO, Not In Parent Scope which is Dispatchers.Main
        // Thus, this function would directly return without waiting for loop completion and will return 0
        CoroutineScope(Dispatchers.IO).launch {
            for (i in 0 until 100) {
                kotlinx.coroutines.delay(10)
                result++
            }
        }
        return result
    }
}

输出: Result : 0

这是一个 非结构化并发 的示例,其中不能保证子协程在返回之前完成。因此,caller/parent 协程会得到子协程返回的错误值。甚至,当子协程已经返回时,子协程可能运行(处于活动状态)在后台,这在某些情况下可能导致内存泄漏

解法:

当我们需要在多个协程之间进行通信时,我们需要确保结构化并发(推荐)

这可以通过在 child/callee 协程中重新使用 parent/caller 协程范围来完成。这可以通过 child/callee 协同程序中的 coroutineScope {} (较小的 c)版本来实现。

private suspend fun downloadUserData(): Int {
    var result = 0
    // By using coroutineScope (Smaller c version) below, we ensure that this coroutine would execute in the
    // parent/caller coroutine's scope, so it would make sure that the for loop would complete
    // before returning from this suspended function. This will return 20000 properly
    coroutineScope {
        for (i in 0 until 100) {
            kotlinx.coroutines.delay(10)
            result++
        }
    }
    return result
}

输出:Result : 100

在非结构化并发示例中,如果您将 Launch 构建器替换为 Async 并等待延迟,它将与您在结构化并发中使用的示例相同。你的答案仍然不清楚。解释结构化并发的实际使用(这在异常和错误处理中很有用,当其中一个子作业抛出异常时,不应影响其他子作业(作业))

它们是完全不同的两个东西。

CoroutineScope 接口 定义协程作用域的概念:要启动和创建协程,您需要一个协程。

例如,

GlobalScope 是一个全局范围实例。

CoroutineScope() 是一个 全局函数 ,它创建一个 CoroutineScope

当你有一个范围时,你可以执行 launch()async() 或与执行协程相关的任何其他方法。

// create a context
val myContext = Dispacher.IO
// you can combine dispachers, parent jobs etc.
// create the new scope
val myScope: CoroutineScope = CoroutineScope(myContext)
// returns immediately (unless you specify a start mode that run immediately)
val job = myScope.launch {
  // suspend calls are allowed here cause this is a coroutine
}
// this code is executed right away

您可以从协程外部执行此操作(纯代码)。

另一方面,

coroutineScope() 是一个 全局挂起函数 ,它在后台创建一个新的 CoroutineScope,然后执行您传递的挂起函数它在正文中,并等待它(及其所有 children)在返回之前完成。它是一个挂起函数,因此您不能在协程之外调用它。

// must be inside a coroutine here!

// this create a new CoroutineScope,
// then launch the given coroutine,
// then wait for it to complete
val result = coroutineScope {
   // your coroutine here, which run immediately
   return@coroutineScope "my result"
}
// this code is executed after the coroutine above is completed
// I can use "result" here

类似于coroutineScopesupervisedScope,只有1个区别:多个children协同程序(启动/异步/ ...)在其中执行不会取消其他children 如果失败导致它使用 SupervisorJob