- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在 Android 中运行下面的代码时,它运行良好。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runBlocking {
launch {
Log.d("Track", "main runBlocking pre : ${Thread.currentThread().name}")
delay(500)
Log.d("Track", "main runBlocking post : ${Thread.currentThread().name}")
}
}
}
它打印
Track: main runBlocking pre : main
Track: main runBlocking post : main
但是,如果我将 Main 上下文提供给
runBlocking
,正如我在下面
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runBlocking(Dispatchers.Main) { // Provide Dispatchers.Main
launch {
Log.d("Track", "main runBlocking pre : ${Thread.currentThread().name}")
delay(500)
Log.d("Track", "main runBlocking post : ${Thread.currentThread().name}")
}
}
}
它挂起而不运行它。
Dispatchers.Main
正在使用
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
为什么挂了?
Dispatchers.Main
到
runBlocking
是让它在主线程中运行,这与提供
Dispatchers.Main
相同。 .我是不是理解错了?
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.21"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2'
最佳答案
runBlocking
的默认调度程序是一个自定义调度程序,它使用调用它的线程来运行协程的延续。这就是当您从 runBlocking
中登录时的原因。协程 block ,它报告它在主线程上。
但是,这与在 Dispatchers.Main.
上运行某些东西不同。 Dispatchers.Main
通过将代码块作为可运行文件发布到主处理程序,从字面上处理协程延续。但是,该处理程序当前正在运行此 onCreate
方法,并且在 onCreate
之前无法处理其队列中的其他消息返回。但是runBlocking
在其子协程返回之前不会返回,所以 onCreate
永远不能回来。
默认 runBlocking
另一方面,调度程序甚至在返回之前直接在当前线程上运行延续。它甚至不知道处理程序。如果你启动一个协程到 Dispatchers.main
来自runBlocking
使用它的默认调度程序,我想你会有同样的挂起。
关于android - 为 runBlocking 提供 Dispatches.Main 挂起 Android App。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65447319/
我正在尝试根据 repo 调用的响应数据运行一个函数,并遇到竞争条件/使用协程范围返回数据的问题。基于这两个伪代码块,我想知道是否可以得到一些帮助? 选项 1:如果不使用 runBlocking,则无
我正在尝试创建一个基本的生成序列 - 必须创建 block ,moveDownLeft,然后是 removeLeft。当之前使用 self.addChild(block1) 添加 block 时,mo
抱歉,对 swift 还很陌生,而且一般都是编码,所以这可能是一个初学者问题。我目前有下面的代码围绕一个圆圈创建箭头。我如何转换它以便所有箭头都在最后一个箭头后 1 秒生成,直到它们全部创建?另一个线
这是我的代码: ship.runAction(SKAction.waitForDuration(5), completion: { self.ship.flyStraight()//retai
我在 Kotlin 中的 await 有问题,如果我只使用 2 个 awaits,这个 runBlocking block 可以工作 10 秒,但是当我尝试使用 4 个 awaits,它工作20秒。据
编辑 2:我想我误解了文档。我读: runBlocking This function should not be used from a coroutine. It is designed to b
我想了解 kotlin 中的 runBlocking。 println("before runBlocking ${Thread.currentThread().name}") runBlo
我正在尝试理解协程,但似乎比预期的更难理解,也许有人可以给我正确的方法。 我想要一个端点(简单的 hello world)来调用挂起的函数。 为此,我做了这个: @GET @Path("/test")
来自 the documentation of runBlocking 是否清楚为什么从协程中使用它没有意义,例如嵌套它。 它甚至明确指出: This function should not be u
let randomize = SKAction.runBlock({ [unowned self] in self.footstepFile = "Content/foots
我有几个需要按顺序运行的 runBlock,但即使在使用 SKAction.sequence([action1, action2, action3]) 时发现它们仍然同时运行。任何帮助将不胜感激。 这
我对 Swift 还很陌生。我试图永远运行一段动画代码。我在这里做错了什么?它一直提示“调用中缺少完成参数”。 func randomCGFloat() -> CGFloat { var te
我是 Swift 的新手,但我在类和继承方面遇到了麻烦。我有一个名为 Bunny 的类,其中包含一些代码,包括 SKAction。当我将在 Bunny 中找到的代码放入我的 GameScene 类时,
如何停止 runBlock 的所有操作: func slideShowControl () { let noObject = SKSpriteNode() noObject.name
这是我正在玩的 SpriteKit 测试应用程序中的一些简单代码: let wait = SKAction.waitForDuration(1) let perform = SKAction.runB
我一直在尝试在我的游戏中实现以下功能。我在 sprite 节点上运行一个 Action (sprite 节点父级为 nil),该 Action 应该等待我指定的时间,然后将节点添加到 self。我找不
我正在学习 Kotlin 协程。我读过 runBlocking 是桥接同步和异步代码的方法。但是如果 runBlocking 停止 UI 线程,性能提升是多少?比如我需要在Android中查询一个数据
MainCoroutineRule和 runBlocking Kotlin Coroutines 都是为测试目的而设计的。似乎两者都提供相同的功能:在测试环境中同步运行代码。 那么有什么区别呢?他们每
Kotlin 的 runBlocking Coroutine 应该阻塞当前线程,直到块内的 Coroutine 完成执行,但是当块内的 Coroutine 是 GlobalScope.launch 时
I have switched my context to Dispatcher.Main to show UI but data fetched on runBlocking but unable
我是一名优秀的程序员,十分优秀!