Sam
03/19/2024, 9:17 AMDispatchers.Main
quite unintuitive 😞. In a simple Swing app, the main (initial) thread and the UI thread are two different things. I'm trying to write an article talking about the two, and I literally can't find a nice way to explain it.
suspend fun main() {
println(Thread.currentThread()) // "Thread[main,…]" <- obviously the main thread, it says so in the name
withContext(Dispatchers.Main) {
println(Thread.currentThread()) // "Thread[AWT-EventQueue-0,…]" <- UI thread
}
}
Anybody got any suggestions for an intuitive way to explain it to newcomers? So far I'm stuck at "Dispatchers.Main runs code on the UI thread, which is not the same as the main thread, but it's called that because on Android it runs code on the main thread, which is usually (but not always) the same as the UI thread" 🤦😂 😭Sam
03/19/2024, 9:30 AMDispatchers.Swing
for my examples 😂Sam
03/19/2024, 9:33 AMDispatchers.Swing
, and then explain that Dispatchers.Main
can be used as an alias if you secretly wish Swing was AndroidSam
03/19/2024, 9:33 AMstreetsofboston
03/19/2024, 1:56 PMRobert Williams
03/20/2024, 10:19 AMmain
thread because you can't actually write a main function on AndroidRobert Williams
03/20/2024, 10:20 AM