Add "virtual single thread context"


#1

What does JB team think about adding “virtual single thread context” for JVM coroutines?

For current moment we can to create context, which is executed only on the single thread - newSingleThreadContext. This context has major advantage: all synchronous executions could not be executed from multiple threads, e.g. you can avoid external synchronization, because of have separate thread.

The main disadvantage of the newSingleThreadContext - it creates separate thread, e.g. JVM will allocate additional resources for this context, any dispatching on it will be expensive, etc.

Possible, it is better to allow creation newVirtualSingleThreadContext, which will use common thread pool, however it will guarantee, that there are not any parallel executions of any synchronous functions.

Of course, this can be done by using non-blocking Mutex, however it is not too simple to verify, are you on the separate thread context, or not.


#2

See https://github.com/Kotlin/kotlinx.coroutines/issues/261