Professional Documents
Culture Documents
EDT in Swings
EDT in Swings
2) InvokeLater is a non-blocking call while InvokeAndWait will block until the task is completed.
3) If the run method of Runnable target throws an Exception then in the case of invokeLater EDT threads
unwinds while in the case of the invokeAndWait exception is caught and rethrown
as InvocationTargetException.
4) InvokeLater can be safely called from Event Dispatcher thread while if you call invokeAndWait from
EDT thread you will get an error because as per java documentation of invokeAndWait it clearly says that
"this request will be processed only after all pending events" and if you call this from EDT this will
become one of the pending events so its a deadlock because the caller of InvokeAndWait is waiting for
the completion of invokeAndWait while EDT is waiting for the caller of InvokeAndWait.
5) InvokeLater is more flexible in terms of user interaction because it just adds the task in queue and
allows the user to interact with the system while invokeAndWait is a preferred way to update the GUI
from application thread.
In Summary, we can just say that since Swing is not thread-safe and we cannot update different Swing
GUI components on any thread other-than Event dispatcher Thread we need to use InvokeAndWait or
InvokeLater to schedule any Runnable task for AWT Event dispatcher Thread. InvokeAndWait is
synchronous and blocking call and waits until submitted Runnable completes while InvokeLater is
asynchronous and non-blocking it will just submit task and exit."