【发布时间】:2019-02-17 14:55:53
【问题描述】:
以优雅的方式组织应用程序协程的最佳方式是什么?我明白了,这个问题似乎很奇怪。让我展示一下Executors
的例子创建一个对象 AppExecutors.kt
object AppExecutors {
private val main: Executor = MainThreadExecutor()
private val util: Executor = Executors.newFixedThreadPool(3)
fun main(f: () -> Unit) {
main.execute(f)
}
fun util(f: () -> Unit) {
util.execute(f)
}
class MainThreadExecutor : Executor {
private val mainThreadHandler = Handler(Looper.getMainLooper())
override fun execute(command: Runnable?) {
mainThreadHandler.post(command)
}
}
}
现在,我们可以使用它了。简单、最少的代码等
val exe = AppExecutors
exe.util {
val first = calculateFirst()
val second = calculateSecond()
val str = ("first = $first | second = $second")
exe.main {
Toast.makeText(activity, "Executors $str", Toast.LENGTH_LONG).show()
}
}
现在,我尝试在协程上使用这种方法 AppCoRoutines.kt
object AppCoRoutines{
private val uiContext: CoroutineContext = Dispatchers.Main
private val ioContext: CoroutineContext = Dispatchers.IO
private val networkContext: CoroutineContext = Executors.newFixedThreadPool(3).asCoroutineDispatcher()
private val singleContext: CoroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
val ui: CoroutineScope = CoroutineScope(uiContext)
val io: CoroutineScope = CoroutineScope(ioContext)
val net: CoroutineScope = CoroutineScope(networkContext)
val single: CoroutineScope = CoroutineScope(singleContext)
}
现在,使用这个:
val coRout = AppCoRoutines
coRout.ui.launch {
val str: String = withContext(coRout.net.coroutineContext){
val first = async { calculateFirst() }
val second = async { calculateSecond() }
("first = $first | second = $second")
}
Toast.makeText(activity, "CoRoutine $str", Toast.LENGTH_LONG).show()
}
一点也不优雅。也许有人可以提出更简单的方法?我现在在这方面不太擅长,所以我只使用协程来完成简单的任务。
提前谢谢你!
【问题讨论】:
-
为什么要创建自己的执行器?协程开箱即用。 GlobalScope.launch(Dispatchers.Main) { val str: String = withContext(Dispatchers.IO) { val first = async { calculateFirst() } val second = async { calculateSecond() } ("first = $first | second = $second ") } Toast.makeText(activity, "CoRoutine $str", Toast.LENGTH_LONG).show() }
-
您好!谢谢您的回复!问题不在于我为什么要这样做,而在于我如何以简单的方式做到这一点,正如我在 Executors 示例中展示的那样。谢谢!
标签: android kotlin kotlinx.coroutines