https://kotlinlang.org logo
#getting-started
Title
# getting-started
f

F0X

12/27/2020, 12:26 PM
Im just starting out with coroutines, so this may be a weird question. Is it possible and how would I go about implementing a
MainCoroutineDispatcher
that simply runs the coroutines on an existing main thread?
That is what I mean, I want to register my own
Main
dispatcher to use one existing, plain java thread. I am not working in an environment where this already exists, like Android, Swing or JavaFX
Look I don't think you understand,
Dispatchers.Main
does not exist without someone registering the appropriate service, which is what I want to do. Because I am integrating this code into an existing Program as a kind of "Plugin" I want to create a simple Dispatcher that runs all coroutines on this single existing thread.
d

Dominaezzz

12/27/2020, 1:44 PM
I think what you're looking for is
runBlocking
which creates a dispatcher on the calling thread for the coroutines to run. If you're looking to create a dispatcher object, I think that can be done in a useful way since the dispatcher has to take control of the thread by blocking it.
f

F0X

12/27/2020, 3:06 PM
Turned out the underlying program already chose to have their own
Executor
for running things on the main thread, so I could just do as @Chrimaeon suggested and delegate to this. Even though it seems a bit hacky using this "InternalCoroutinesApi" to set
Dispatchers.Main
myself everything is working fine so far. Thank you all for helping out!
👍 1
2 Views