簡體   English   中英

如何創建在我的 ThreadPoolExecutor 上工作的自定義 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM