`
kingsword
  • 浏览: 7545 次
社区版块
存档分类
最新评论

AppExecutor

 
阅读更多

1. 全局的单例
2. 里面管理了diskIO executor, networkIO executor和main executor. 操作数据库用diskIO,网络请求用networkIO.

class AppExecutors(
    private val diskIO: Executor,
    private val networkIO: Executor,
    private val mainThread: Executor
) {

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

    fun diskIO(): Executor {
        return diskIO
    }

    fun networkIO(): Executor {
        return networkIO
    }

    fun mainThread(): Executor {
        return mainThread
    }

    private class MainThreadExecutor : Executor {
        private val mainThreadHandler = Handler(Looper.getMainLooper())
        override fun execute(command: Runnable) {
            mainThreadHandler.post(command)
        }
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics