- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的很高兴我将长期运行的任务切换到了协程,这些任务不断地产生UI线程的结果。与 AsyncTask 或 Android 中的常规线程相比,它提高了性能并将内存使用量减少了 3 倍,并且所有内存泄漏都消失了。
唯一的问题是,我不知道如何在异常在某个时间发生后重新启动我的长时间运行的操作......
阅读了大量文章后,我觉得我根本不了解协程中的异常处理。让我知道如何实现所需的行为。
lateinit var initEngineJob: Job
override val coroutineContext: CoroutineContext
get() = initEngineJob + Dispatchers.Main
fun initWorkEngineCoroutine()
{
launch {
while(true) {
val deferred = async(Dispatchers.Default) {
getResultsFromEngine()
}
val result = deferred.await()
if (result != null) {
//UI thread
draw!!.showResult(result)
}
}
}
}
fun getResultsFromEngine() :Result? {
result = // some results from native c++ engine, which throws exception at some times
return result
}
我不知道我应该把 try catch 放在哪里。我尝试用 try catch 包围 deferred.await(),但我无法不在 catch block 中调用相同的方法 以重试长时间运行的任务。我尝试了 SupervisorJob(),但也没有成功。我仍然无法再次调用initWorkEngineCoroutine() 和启动新协程...
终于帮忙解决了这个问题:)
最佳答案
您应该将您的代码视为线性命令式,并在代码中最符合逻辑的地方尝试/捕获。有了这种心态,您的问题可能不再是关于协同程序,而是更多关于 try/catch 重试。你可能会这样做:
fun main() {
GlobalScope.launch {
initWorkEngineCoroutine()
}
}
suspend fun initWorkEngineCoroutine() {
var failures = 0
val maxFailures = 3
while(failures <= maxFailures) {
try {
getResultsFromEngine()?.let {
draw!!.showResult(it)
}
} catch (e: Exception) {
failures++
}
}
}
// withContext is like async{}.await() except an exception occuring inside
// withContext can be caught from inside the coroutine.
// here, we are mapping getResultFromEngine() to a call to withContext and
// passing withContext the lambda which does the work
suspend fun getResultsFromEngine() :Result? = withContext(Dispatchers.Default) {
Result()
}
我加入了一些逻辑来防止无限循环。它可能不符合您的要求,但您可能会考虑采取某种措施来防止 getResultsFromEngine()
立即引发异常并最终导致可能导致意外行为的无限循环的问题和潜在的计算器溢出。
关于android - 我应该如何在 Android 异常后重新启动/重试 Kotlin 协程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55921068/
在我的设置中,我试图有一个界面 Table继承自 Map (因为它主要用作 map 的包装器)。两个类继承自 Table - 本地和全局。全局的将有一个可变的映射,而本地的将有一个只有本地条目的映射。
Rust Nomicon 有 an entire section on variance除了关于 Box 的这一小节,我或多或少地理解了这一点和 Vec在 T 上(共同)变体. Box and Vec
我是一名优秀的程序员,十分优秀!