Threading
The UI thread that runs Compose under Nucleus, which callbacks reach it, how to port Swing-style code, and the Windows event-loop watchdog.
nucleusApplication runs the event loop on the thread that calls it, main in a standard app.
That UI thread handles input, composition and drawing for every window, and Dispatchers.Main
points at it, so LaunchedEffect, rememberCoroutineScope() and view-model scopes resume there.
On macOS, AppKit requires the process's first thread. When main runs on another thread, the
loop moves to the first thread and Dispatchers.Main follows it, so Dispatchers.Main.immediate
still runs inline from UI callbacks. main stays blocked until the app quits.
The AWT event dispatch thread (EDT) still exists, but it is a different thread. Code posted to it runs outside Compose's thread.
Native callbacks on the UI thread
These modules call your code on the UI thread, so you can update Compose state directly:
| Module | Callbacks |
|---|---|
notification-common (macOS), notification-windows, notification-linux | Notification activations, actions, dismissals and failures |
media-control | Play, pause, next, previous, stop, seek |
launcher-macos / launcher-linux | Dock menu and Unity quicklist item clicks |
menu-macos | Menu actions and delegate events |
global-hotkey | Hotkey presses and releases |
Changed in 3.0
These callbacks used to run on the AWT EDT. Code that assumed the EDT, such as
SwingUtilities.isEventDispatchThread() checks, must change. See
Migrate from 2.x.
Other callbacks run on the thread that received the event, such as the notification-macos
delegate. Post to the UI thread with NucleusUiThread before touching UI state:
NucleusUiThread.post { viewModel.onNotificationClicked(id) }post always queues the block, even from the UI thread, so callback order stays the same.
Port Swing-style code
| Swing-style code | Replace with |
|---|---|
SwingUtilities.invokeLater { }, EventQueue.invokeLater { } | withContext(Dispatchers.Main) { } or NucleusUiThread.post { } |
Dispatchers.Swing | Dispatchers.Main |
SwingUtilities.isEventDispatchThread() | Drop the check, or dispatch to Dispatchers.Main |
javax.swing.Timer | LaunchedEffect with delay |
A Swing JFrame can still run beside Nucleus windows, on the EDT. Reach it with
SwingUtilities.invokeLater and come back with Dispatchers.Main.
Detect a stalled event loopNew
On Windows, Nucleus logs SEVERE with a full thread dump when a window stops responding. Register
handlers to react, for example to report the hang:
fun main(args: Array<String>) = nucleusApplication(args) {
onUnresponsive { crashReporter.reportHang() }
onResponsive { crashReporter.hangEnded() }
// ...
}- Both handlers run on a dedicated watchdog thread, since the UI thread is the stuck one. Work you
post to
Dispatchers.Mainfrom them waits until the stall ends. onResponsivefires only after a stall thatonUnresponsivereported.- Each call replaces the previous handler, so calling them from the application block is safe.
- Nucleus shows no dialog by default.
Wrap long synchronous UI-thread work in expectUnresponsive so it is not reported. It returns the
block's result and works from any thread:
expectUnresponsive { importHugeProjectSynchronously() }| System property | Effect |
|---|---|
-Dnucleus.tao.watchdog=false | Disables the watchdog. true forces it on under a debugger. |
-Dnucleus.tao.watchdogGraceMs=<ms> | Extra time before a stall is reported. Default 5000. |
-Dnucleus.tao.watchdogDialog=true | Also shows the native error dialog on a stall. |
What's next
- Architecture: the layers of a Nucleus app.
- Notifications: callbacks that now land on the UI thread.
- Media control: media key and Now Playing events.
- Migrate from 2.x: every 3.0 change in one list.
Last updated on