• Post Reply Bookmark Topic Watch Topic
  • New Topic

What needs to/doesn't need to be run on the EDT?

 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Due to the way AWT/Swing works, code that updates GUI elements should be run on the EDT. However, I'm not sure what doesn't need to be run on the EDT. For example, amongst other things, when initializing a window (like a JFrame, for instance), or setting its dimensions/location before it is first displayed/made visible, need it be run on the EDT? Should I just wrap everything in a SwingUtilities.invokeLater call?
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it does need to be run on the EDT. The only thing snot to run on the EDT shou‍ld be long‑running tasks which are completely independent of the display. The threads for such tasks are often called worker threads. Have a look in the Java™ Tutorials.
By the way: calling invokeLater simply starts the EDT, so that bit sounds correct.
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way: calling invokeLater simply starts the EDT, so that bit sounds correct.

To be pedantic, it's usually Component.setVisible() or Window.pack() that start the EDT. invokeLater() just adds tasks to the event queue.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting point.  What thread does the task started by invokeLater() execute on if the EDT hasn't been started by a method like: Component.setVisible() or Window.pack()?
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just speculating here, but I imagine nothing happens, and the task just remains in the queue until the EDT is started.
 
Norm Radder
Bartender
Posts: 1526
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When in doubt, write a test program:
This was in my non-GUI main for testing.  The output:

Running: java.exe -client -cp . TestCode22

This might well be displayed before the other message.
>>> main exiting <<<
Hello World on Thread[AWT-EventQueue-0,6,main]

0 error(s)
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whelp, so much for speculation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!