You are on page 1of 2

invokeLater and invokeAndWait

A Swing programmer deals with the following kinds of threads:


 Initial threads, the threads that execute initial application code.
 The event dispatch thread, where all event-handling code is executed. Most code that interacts
with the Swing framework must also execute on this thread.
 Worker threads, also known as background threads, where time-consuming background tasks are
executed.
The programmer does not need to provide code that explicitly creates these threads: they are provided by
the runtime or the Swing framework. The programmer's job is to utilize these threads to create a
responsive, maintainable Swing program.
Initial threads
In Swing programs, the initial threads don't have a lot to do. Their most essential job is to create
a Runnable object that initializes the GUI and schedule that object for execution on the event dispatch
thread. Once the GUI is created, the program is primarily driven by GUI events, each of which causes the
execution of a short task on the event dispatch thread. Application code can schedule additionals tasks on
the event dispatch thread (if they complete quickly, so as not to interfere with event processing) or a
worker thread (for long-running tasks).
An initial thread schedules the GUI creation task by
invoking javax.swing.SwingUtilities.invokeLater or javax.swing.SwingUtilities.invokeAndWait . Both of
these methods take a single argument: the Runnable that defines the new task. Their only difference is
indicated by their names: invokeLater simply schedules the task and returns; invokeAndWait waits for the
task to finish before returning.
You can see examples of this throughout the Swing tutorial:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
In an applet, the GUI-creation task must be launched from the init method using invokeAndWait;
otherwise, init may return before the GUI is created, which may cause problems for a web browser
launching an applet. In any other kind of program, scheduling the GUI-creation task is usually the last
thing the initial thread does, so it doesn't matter whether it uses invokeLater or invokeAndWait.
The Event Dispatch Thread
Swing event handling code runs on a special thread known as the event dispatch thread. Most code that
invokes Swing methods also runs on this thread. This is necessary because most Swing object methods
are not "thread safe": invoking them from multiple threads risks thread interference or memory
consistency errors. Some Swing component methods are labelled "thread safe" in the API specification;
these can be safely invoked from any thread. All other Swing component methods must be invoked from
the event dispatch thread. Programs that ignore this rule may function correctly most of the time, but are
subject to unpredictable errors that are difficult to reproduce.

1) InvokeLater is used to perform a task asynchronously in AWT Event dispatcher thread while


InvokeAndWait is used to perform task synchronously.

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."

You might also like