Kotlin 协程调度程序类似于 Default 但没有 Main (UI) Thread

Kotlin coroutines dispatcher like Default but without a Main (UI) Thread

有没有办法在 Android 上创建(或以某种方式获得)Kotlin 协程调度程序,其行为类似于默认但不包括接触 UI 线程核心?

所以基本上,我面临着一个繁重的计算任务,我想用尽可能多的 CPU 资源来处理,而不影响 UI 性能。

使用 Dispatchers.Default 我从系统中获得了最多的资源,但与此同时,我的 UI 变得更慢了。

这里可以应用哪些最佳实践?

您可以像这样使用带有自定义线程池的调度程序:

val threads = 4
val dispatcher = Executors.newFixedThreadPool(threads).asCoroutineDispatcher()

事实证明,我的问题解决方案不在 JVM 领域,而是在 NDK 领域。本机库正在加载 CPU 到 100%,基本上看起来我从 Java/Kotlin 开始就无能为力了。看起来答案在 C 代码的某处,这超出了我的能力范围。

我还必须提到 Executors.newFixedThreadPool((Runtime.getRuntime().availableProcessors() - 1).coerceAtLeast(1)).asCoroutineDispatcher() 解决方案。在我看来,它可以帮助遇到类似问题的人。