bob
05/04/2024, 11:16 AMExecutors.newSingleThreadExecutor { Thread.currentThread() }.asCoroutineDispatcher()
But I'm getting java.lang.IllegalThreadStateException
when using that dispatcher.Sam
05/04/2024, 11:23 AMrunBlocking
?bob
05/04/2024, 11:58 AMDispatchers.Main
but using the thread I'm currently on.Sam
05/04/2024, 12:20 PMbob
05/04/2024, 12:55 PMSam
05/04/2024, 12:59 PMbob
05/04/2024, 1:10 PMstreetsofboston
05/04/2024, 2:41 PMandylamax
05/06/2024, 3:13 PMclass SynchronousExecutor : AbstractExecutorService(), ExecutorService {
var active = true
override fun execute(command: Runnable) {
command.run()
}
override fun shutdown() {
active = false
}
override fun shutdownNow(): MutableList<Runnable> {
active = false
return mutableListOf()
}
override fun isShutdown(): Boolean = !active
override fun isTerminated(): Boolean = !active
override fun awaitTermination(timeout: Long, unit: TimeUnit): Boolean {
return true
}
}
fun synchronousDispatcher() = SynchronousExecutor().asCoroutineDispatcher()
suspend fun test() {
withContenext(synchronousDispatcher()) {
// do your thing here
}
}
Sam
05/06/2024, 3:16 PMandylamax
05/06/2024, 3:19 PMSam
05/06/2024, 3:24 PMexecute
will not be called from the original thread after a resumeandylamax
05/06/2024, 3:36 PMexecute
is the method responsible for switching threads. Not execute
's caller. Am I wrong?Sam
05/06/2024, 4:21 PMexecute
will simply be called from whichever thread calls resume
-- not from the thread that created the executorandylamax
05/06/2024, 4:27 PMresume
be called from a separate thread??Sam
05/06/2024, 4:34 PMresume
. So the thread varies depending on what library provides the underlying callback.Sam
05/06/2024, 4:35 PMDispatchers.Unconfined
can resume on an arbitrary thread, what that really means is it will just resume directly on whatever thread triggers the coroutine to resume.andylamax
05/06/2024, 5:35 PMSam
05/06/2024, 5:41 PM