UI thread¶
Category: Units of execution · Status: stub · Lessons: chapter 06, Async (planned)
One line: The one thread a graphical toolkit allows to touch its widgets: long work goes to other threads or tasks, and its results are posted back to that thread.
Also called: main thread, event dispatch thread, GUI thread.
How it connects¶
flowchart LR
n_thread["Thread"]
n_ui_thread["UI thread"]
n_ui_thread -->|is a| n_thread
classDef center stroke-width:3px
class n_ui_thread center
classDef outside stroke-dasharray: 4 3
class n_thread outside
- Is a kind of: Thread
- See also: Blocking the event loop, Event loop, Thread confinement
In each language¶
| Java | Swing's event dispatch thread: SwingUtilities.invokeLater ↗ runs code on it |
| C# | Control.Invoke ↗ runs a delegate on the thread that owns a Windows Forms control |
| JavaScript | the main thread ↗, where the browser runs user code, handles events and renders; web workers run elsewhere |
| Swift | MainActor ↗, the global actor that performs its tasks on the main thread |
Where to read more¶
- In the books: Java Concurrency in Practice, Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea — ch. 9, 'GUI Applications'
- In the books: Concurrent Programming on Windows, Joe Duffy — ch. 16, 'Graphical User Interfaces'
- In the books: Parallel Programming and Concurrency with C# 10 and .NET 6, Alvin Ashcraft — ch. 4, 'User Interface Responsiveness and Threading'
- In the books: Pro Asynchronous Programming with .NET, Richard Blewett, Andrew Clymer — ch. 6, 'Asynchronous UI'
- In the books: Kotlin Coroutines by Tutorials, Filip Babić, Nishant Srivastava — ch. 1, 'What Is Asynchronous Programming' → 'Interacting with the UI thread from the background'
- In the books: C++ Reactive Programming, Praseed Pai, Peter Abraham — ch. 9, 'Reactive GUI Programming Using Qt/C++'