简体   繁体   English

使用 GlobalScope.launch 和 CoroutineScope().launch 启动协程有区别吗?

[英]Is there a difference between using GlobalScope.launch and CoroutineScope().launch to launch a coroutine?

There are different ways of launching a coroutine in Kotlin.在 Kotlin 中有多种启动协程的方法。 I found a couple of examples where GlobalScope and CoroutineScope are used.我发现了几个使用GlobalScopeCoroutineScope的例子。 But the latter one is being created directly when launching a coroutine:但是后者是在启动协程时直接创建的:

  1. Using GlobalScope :使用GlobalScope

     fun loadConfiguration() { GlobalScope.launch(Dispatchers.Main) { val config = fetchConfigFromServer() // network request updateConfiguration(config) } }
  2. Using CoroutineScope instances, created directly when launching a coroutine:使用CoroutineScope实例,在启动协程时直接创建:

     fun loadConfiguration() { CoroutineScope(Dispatchers.Main).launch { val config = fetchConfigFromServer() // network request updateConfiguration(config) } }

In this case is there a difference between these two approaches?在这种情况下,这两种方法有区别吗?

Doesn't the second case violate the principle of structured concurrency ?第二种情况是不是违反了结构化并发的原则

Doesn't the second case violate the principle of structured concurrency?第二种情况是不是违反了结构化并发的原则?

Actually both cases violate it equally, because they have pretty much the exact same semantics.实际上这两种情况同样违反了它,因为它们具有几乎完全相同的语义。 All the warnings against using GlobalScope.launch that you may have encountered, apply to CoroutineScope().launch just the same.您可能遇到的所有反对使用GlobalScope.launch的警告同样适用于CoroutineScope().launch A slight difference is that the latter is entirely pointless, whereas the former at least has some legitimate use cases.一个细微的区别是后者完全没有意义,而前者至少有一些合法的用例。

Writing CoroutineScope().launch instead of GlobalScope.launch obscures the intent and consequences, as well as creates a needless object every time.编写CoroutineScope().launch而不是GlobalScope.launch会掩盖意图和后果,并且每次都会创建不必要的 object。

I think the CoroutineScope().launch idiom came into being solely because it circumvented the warnings you get for GlobalScope.launch .我认为CoroutineScope().launch成语的出现仅仅是因为它规避了您对GlobalScope.launch的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用协程时使用 GlobalScope.launch 的效果 - Effect of using GlobalScope.launch while using coroutine view.post() 和 GlobalScope.launch(Dispatchers.Main) 有什么区别? - What's the difference between view.post() and GlobalScope.launch(Dispatchers.Main)? 在jetpack compose中替换GlobalScope.launch - replace GlobalScope.launch in jetpack compose GlobalScope.launch 是“微妙的” - 如何处理? - GlobalScope.launch is "delicate" - how to deal with? GlobalScope.launch(Main) 中的同步函数:Kotlin - Synchronise function inside GlobalScope.launch(Main): Kotlin 如何从“GlobalScope.launch”块获取字符串作为返回值 - How to get String as a return value from "GlobalScope.launch" block 为什么 Kotlin 中某些 GlobalScope.launch 调用的内容默默地无法执行? - Why are the contents of some GlobalScope.launch calls in Kotlin silently failing to execute? Google Architecture Sample 中的 coroutineScope { launch { code } } 和 withContext(iODispatcher) { code } 有什么区别? - What is the difference between coroutineScope { launch { code } } and withContext(iODispatcher) { code } in the Architecture Sample by Google? 在 CoroutineScope.launch 之后的 withContext - CoroutineScope.launch 会阻塞吗? - withContext after a CoroutineScope.launch - will CoroutineScope.launch block? 无法启动协程生成器 - cant launch coroutine builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM