![](/img/trans.png)
[英]How can I create single-thread coroutine context under Common pool in Kotlin?
[英]How to create custom Kotlin coroutine dispatcher worked on my ThreadPoolExecutor?
在遷移到 Kotlin 的過程中,問題出現了。
由於不同的原因,我們已經有了一些線程池。
如何在現有的 ThreadPool 上執行協程?
例如這個代碼:
suspend fun fetchMedia(): Flow<MediaItem> {
return withContext(Dispatchers.IO) {...}
}
如何將 Dispatchers.IO 替換為我自己的 ThreadPoolExecutor ?
您可以使用 Executor ThreadPools 在其上調用asCoroutineDispatcher()
,如下所示:
suspend fun fetchMedia(): Flow<MediaItem> {
return withContext(myThreadPool.asCoroutineDispatcher()) {...}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.