Java EventQueue. Why should everything be in the invokelater method?

in the book I'm reading, each multithreaded GUI example has something like this:

public static void main(String[] args) throws Exception { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new SomeKindOfFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } 

(I mean EventQueue). but is the code not automatically executed in the main (EDT) thread?

+7
java multithreading swing event-dispatch-thread
Apr 25 '11 at 17:14
source share
2 answers

Desktop GUI applications typically work this way. There is one thread for gui and one or more threads for the rest of the application. Using EventQueue , you specify which thread the GUI should make from other threads.

+6
Apr 25 '11 at 17:28
source share

The main thread does not match the EDT. If you add System.out.println(Thread.currentThread().getName() , you will see that it printed main inside main() and AWT-EventQueue-0 when in the run() method Runnable .

Here's a discussion of the history of a single-threaded rule in Swing that can help make things all the more clear.

+13
Apr 25 '11 at 17:27
source share



All Articles