[英]Transfer Coroutine Dispatcher's Tasks to another Dispatcher
我的問題很簡單,給定 Dispatcher 1,您如何將 Dispatcher 1 的任務轉移到另一個名為 Dispatcher 2 的 Dispatcher?
不知道轉移意味着什么,但是是的,您可以在線程之間跳轉。 您可以在協程中使用withContext
在線程之間切換。 像這樣:
val customContext = newSingleThreadContext("CustomContext")
runBlocking(Dispatchers.Default) {
// Started in DefaultDispatcher
withContext(customContext) {
// Working in CustomContext
}
// Back to DefaultDispatcher
}
runBlocking(Dispatchers.Unconfined) {
// Started in main thread
withContext(Dispatchers.Default) {
// Working in DefaultDispatcher
}
// Back to main thread
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.