Question

I recently found an example code:

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

The createAndShowGUI() method opens a user interface window. Then I tried to trim the code as the following:

public static void main(String[] args) {
    createAndShowGUI();
}

Both versions work equally well. What is the difference?

Was it helpful?

Solution

99% of the time either code will work.

However, Swing was designed such that all updates to Swing components should be done on the Event Dispatch Thread (EDT). Read the Swing tutorial on Concurrency for more information.

The problem is the 1% of the time when it might not work. You don't want to waste time trying to debug random problems.

OTHER TIPS

SwingUtilities.invokeLater ensures that the code executes on the Event Dispatch Thread (EDT). Swing is single-threaded, all component instantiation and display should happen on the EDT. Things might seem to work otherwise, but you'll possibly run into issues.

Swing has single threaded Event Loop based design and is not thread safe, with only thread safe part being a set of invokeXXX() methods used to transfer control to the Swing event loop.

Your "broken" code works just fine because nothing else is trying to touch the same part of Swing data structures from within the Event Loop at the same time.

The "main" thread started by the JVM is not the Event Dispatch Thread.

from The Event Dispatch Thread

Some Swing component methods are labelled "thread safe" in the API specification; these can be safely invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread. Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top