1

This is a really simple question about the invokeAndWait thing from swing utilities. I have heard that it synchronizes code execution on a single thread, but I'm not sure. If so, should I use invokeAndWait to do that?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Boogley Beegly
  • 95
  • 1
  • 3
  • 11
  • 1
    As usual, the API tells what you need to know and more. Please give it a look: [SwingUtilities#invokeAndWait](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeAndWait(java.lang.Runnable)) – Hovercraft Full Of Eels Feb 26 '13 at 03:21

1 Answers1

3

SwingUtilities.invokeAndWait(Runnable) will enqueue the Runnable on the Event Queue. This will allow the Event Dispatching Thread to execute the run method of the Runnable within the context of the Event Dispatching Thread.

invokeAndWait will not return until AFTER the EDT has finished executing the run method. This means it's a blocking operation.

invokeAndWait is used to re-sync code to the EDT, allowing it to execute updates to the UI within the Swing toolkit.

Unless you are trying to get you code to be executed on the EDT, no, you shouldn't use it for thread synchronization.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    most important is invokeAndWait caused an exception if is EDT present and non empty, have to test isEventDispatchThread before usage of – mKorbel Feb 26 '13 at 07:46