javaswingedt

Why EventDispatchThread in first place?


This is the design decision I don't understand.

Both Android and JME follow the policy that the thread that started an app is the UI thread and you take care to offload resource-consuming stuff to another threads.

In Swing, on the other hand, you use EventQueue.invokeLater(Runnable) for UI and SwingWorker for background processing.

Now, what's the main thread for?


Solution

  • As mentioned in this Sun article about thread, you can do whatever you want in the main thread, including building a GUI, even though it is risky.

    Back to the question:

    Swing has not been implemented with the main thread solely related to GUI because that would force a pure multi-thread approach and:

    So the main thread can be used for initialization (of data and GUI, provided they do not take too much time), while most post-initialization GUI steps naturally occurs in the event-dispatching thread.
    Once the GUI is visible, most programs are driven by events such as button actions or mouse clicks, which are always handled in the event-dispatching thread..