- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这两种方法有什么区别吗?
runBlocking {
launch(coroutineDispatcher) {
// job
}
}
GlobalScope.launch(coroutineDispatcher) {
// job
}
最佳答案
runBlocking
runs new coroutine and blocks current thread interruptibly until its completion. This function should not be used from coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests.
// line 1
runBlocking {
// line 2
launch(coroutineDispatcher) {
// line 3
}
// line 4
}
// line 5
someFunction()
runBlocking
代码行将按以下顺序执行:
line 1
line 2
line 4
line 3
line 5 // this line will be executed after coroutine is finished
Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely. Another use of the global scope is operators running in Dispatchers.Unconfined, which don't have any job associated with them. Application code usually should use application-defined CoroutineScope, using async or launch on the instance of GlobalScope is highly discouraged.
// line 1
GlobalScope.launch(coroutineDispatcher) {
// line 2
}
// line 3
someFunction()
GlobalScope.launch
代码行将按以下顺序执行:
line 1
line 3
line 2
runBlocking
阻塞当前线程直到它完成,
GlobalScope.launch
没有。
关于Kotlin 协程 GlobalScope.launch 与 runBlocking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842169/
在我的设置中,我试图有一个界面 Table继承自 Map (因为它主要用作 map 的包装器)。两个类继承自 Table - 本地和全局。全局的将有一个可变的映射,而本地的将有一个只有本地条目的映射。
Rust Nomicon 有 an entire section on variance除了关于 Box 的这一小节,我或多或少地理解了这一点和 Vec在 T 上(共同)变体. Box and Vec
我是一名优秀的程序员,十分优秀!