• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between involeLater() and invokeAndWait()

 
Anil Deshpande
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was going through some of the swing programs.

I came across something like SwingUtilities.invokeLater() SwingUtilities.invokeAndWait(). I did read up some material on that. But was unable understand the concepts of these two methods.


Can some body explain me the difference?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both methods allow you to execute something on the Event Dispatch Thread. invokeLater will add the task to a queue, and you will still be able to interact with your application. On the contrary, invokeAndWait adds the task to a queue, but will wait until that task ends, which means that anything related to the Event Dispatch Thread (repainting...) will freeze.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe is right. Another difference is that you can call invokeLater from the Event Dispatcher Thread (EDT). After all, the Runnable is queued for a later execution. If you try to call invokeAndWait from the EDT you will get an error. The Javadoc of invokeAndWait cleary mentions "This will happen after all pending events are processed." That includes the current event is calling invokeAndWait. You would get invokeAndWait waiting for the current event to finish, and the current event waiting for invokeAndWait to finish. That's a guaranteed deadlock, and that's why it isn't allowed.

Of course Sun could have decided, in the past, to just call runnable.run() and be done with it if invokeAndWait would be called from the EDT. Now they can't anymore because it would no longer do what the Javadoc says. That doesn't prevent you from writing your own wrapper:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!