Nucleus
Concepts

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.

View as Markdown

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:

ModuleCallbacks
notification-common (macOS), notification-windows, notification-linuxNotification activations, actions, dismissals and failures
media-controlPlay, pause, next, previous, stop, seek
launcher-macos / launcher-linuxDock menu and Unity quicklist item clicks
menu-macosMenu actions and delegate events
global-hotkeyHotkey 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 codeReplace with
SwingUtilities.invokeLater { }, EventQueue.invokeLater { }withContext(Dispatchers.Main) { } or NucleusUiThread.post { }
Dispatchers.SwingDispatchers.Main
SwingUtilities.isEventDispatchThread()Drop the check, or dispatch to Dispatchers.Main
javax.swing.TimerLaunchedEffect 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.Main from them waits until the stall ends.
  • onResponsive fires only after a stall that onUnresponsive reported.
  • 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 propertyEffect
-Dnucleus.tao.watchdog=falseDisables 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=trueAlso shows the native error dialog on a stall.

What's next

Was this page helpful?
Edit on GitHub

Last updated on