Coroutine dispatcher confined to a single thread

Maybe if it was possible to say minus on the CoroutineContext, then I could remove the default dispatcher. Alas, coroutine contexts are only additive.

But am I wrong this is strange?

  • If there is no dispatcher, it will be confined to a single thread.
  • As soon as a dispatcher is added to the context, it cannot be removed, so the default behavior cannot be replicated.

Seems illogical to me. Perhaps if there was Dispatchers.ThisThread?

I have found: Sliceable dispatchers: Provide alternative to newSingle/FixedThreadPoolContext via a shared pool of threads · Issue #261 · Kotlin/kotlinx.coroutines · GitHub which, if implemented, would allow me to do what I want.

Also: Add "virtual single thread context"

1 Like