https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

alexcouch

09/12/2018, 4:54 AM
So I'm curious about something: I'm trying to create a way so that different threads take input but only one thread is active at a time and I can switch between the threads and whatever input is taken goes to that thread. I'm not quite sure how to do it. I kinda did it using the JDK Thread api but it's not quite working out as well as I was hoping cause I need to keep input coming in until the active thread is changed and then the input is sent into the new active thread. I tried to look into coroutines but that only confused me even more. Any ideas?
Nevermind, I forgot about scheduled executors
d

Dico

09/12/2018, 5:35 AM
If scheduled executors are the answer to your question, I certainly didn't understand it. However, by implenenting your own
CoroutineDispatcher
, you get to write the code that submits tasks to executors. The easiest way to do this would be like so:
```
Copy code
class MyCoroutineDispatcher(override var executor: Executor) : ExecutorCoroutineDispatcherBase()
val DISPATCHER = MyCoroutineDispatcher(Executors.newSingleThreadExecutor())
Now you can modify the
executor
used. It might be a good idea to mark the
executor
property
@Volatile
. By using only single threaded executors, you can control exactly which thread is used.
2 Views