groostav
11/13/2025, 6:27 AMDispatchers.Main but dont want to put javafx on my classloader, what are my options? Do I have any, or do i need to roll my own MainCoroutineDispatcher implementation and insert it in the SPI/service-loader?Michael Paus
11/13/2025, 10:09 AMDispatchers.Main comes in via
implementation(libs.kotlinx.coroutines.swing)
which has nothing to do with JavaFX, or did I misunderstand your question?groostav
11/13/2025, 7:49 PMMainDispatcherFactory, https://github.com/Kotlin/kotlinx.coroutines/blob/5d4954b17ac18c572e22a9a6e7bb4e43[…]nx-coroutines-core/common/src/internal/MainDispatcherFactory.kt
The thing im noticing is that it only exists for UI frameworks (android, javafx, swing, etc).
If im writing a desktop CLI app and I want a Main (single-threaded for thread-confinement-ee things), and I dont want to depend on a UI framework, what do I do?Michael Paus
11/13/2025, 8:11 PMlouiscad
11/14/2025, 2:38 AMgroostav
11/18/2025, 2:05 AMwithContext(Dispatchers.Main) --trying to use Main as a kind of thread confinrement. This works well in the GUI situtation because Main is my UI thread which is perfect for thread confinement.
But this breaks for the CLI and API. For the CLI i just didnt notice that it was always using the JavaFX event loop. This happened to work in a lot of environments it probably should've failed in; im curios as to how javafx was able to start from a machine without X or GTK installed. But from the API this is a disaster.
So: What should I update my state-mutating code to do? How can I give it a `Dispatchers.Main`thats sufficiently thread-confine-ing?Andreas Scheja
11/18/2025, 5:47 PMExecutors.newSingleThreadExecutor().asCoroutineDispatcher()? This will give you a single-threaded coroutine dispatcher, much like Dispatchers.Main. Though if you really want to just make sure that something is not called concurrently, why not use a Mutex instead?ephemient
11/21/2025, 3:41 AMDispatchers.Default.limitedParallelism(1) instead of creating a whole new dispatcher and thread pool