相关疑难解决方法(0)

何时以及为什么会使用一组 Executor

我一直在阅读 Android 文档中关于 Executor 的内容。如果我理解正确的话,它用于多线程管理,它会为您完成一些工作,例如在需要时生成新线程。或者你可以选择自己管理东西。

在下面的示例中,使用一组执行程序而不是一个执行程序。所以它就像一个线程池(?)。

/**
* Global executor pools for the whole application.
*
* Grouping tasks like this avoids the effects of task starvation (e.g. disk 
reads don't wait behind
* webservice requests).
*/
@Singleton
open class AppExecutors(
private val diskIO: Executor,
private val networkIO: Executor,
private val mainThread: Executor
) {

@Inject
constructor() : this(
    Executors.newSingleThreadExecutor(),
    Executors.newFixedThreadPool(3),
    MainThreadExecutor()
)

fun diskIO(): Executor {
    return diskIO
}

fun networkIO(): Executor {
    return networkIO
}

fun mainThread(): Executor …
Run Code Online (Sandbox Code Playgroud)

android

4
推荐指数
1
解决办法
989
查看次数

标签 统计

android ×1