Main

A coroutine dispatcher that is confined to the Main thread operating with UI objects. Usually such dispatchers are single-threaded.

Access to this property may throw an IllegalStateException if no main dispatchers are present in the classpath.

Depending on platform and classpath, it can be mapped to different dispatchers:

  • On JVM it is either the Android main thread dispatcher, JavaFx, or Swing EDT dispatcher. It is chosen by the ServiceLoader.

  • On JS it is equivalent to the Default dispatcher with immediate support.

  • On Native Darwin-based targets, it is a dispatcher backed by Darwin's main queue.

  • On other Native targets, it is not available.

  • Dispatchers.setMain from the kotlinx-coroutines-test artifact can replace the main dispatcher with a mock one for testing.

In order to work with the Main dispatcher on the JVM, the following artifact should be added to the project runtime dependencies:

  • kotlinx-coroutines-android — for Android Main thread dispatcher

  • kotlinx-coroutines-javafx — for JavaFx Application thread dispatcher

  • kotlinx-coroutines-swing — for Swing EDT dispatcher