[英]Transfer Coroutine Dispatcher's Tasks to another Dispatcher
我的问题很简单,给定 Dispatcher 1,您如何将 Dispatcher 1 的任务转移到另一个名为 Dispatcher 2 的 Dispatcher?
Not sure what transfer would mean but yes you can jump between threads .不知道转移意味着什么,但是是的,您可以在线程之间跳转。 You can use
withContext
within a coroutine to switch between threads .您可以在协程中使用
withContext
在线程之间切换。 Like so:像这样:
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.