A software application normally consists of multiple threads and a single GUI data structure. This means GUI is a shared data structure and some synchronization is needed to ensure that only one thread accesses it at a time. Though AWT and Swing expose the methods to create and access the GUI components and these methods are visible to all application threads, likewise in other GUI frameworks, only a single, Event Dispatching thread has the right to execute these methods. Since programmers often miss this requirement, third-party Look and Feels, like go as far as to refuse to instantiate any Swing component when not running within the Event Dispatch Thread, to prevent such a coding mistake. Access to the GUI is serialized and other threads may submit some code to be executed in the EDT through a EDT message queue. That is, likewise in other GUI frameworks, the Event Dispatching Thread spends its life pumping messages: it maintains a message queue of actions to be performed over GUI. These requests are submitted to the queue by system and any application thread. EDT consumes them one after another and responds by updating the GUI components. The messages may be well-known actions or involve callbacks, the references to user-methods that must be executed by means of EDT. The important requirement imposed on all messages is that they must be executed quickly for the GUI to stay responsive. Otherwise, the message loop is blocked and GUI freezing is experienced.
Submitting user code to the EDT
There are various solutions for submitting code to the EDT and performing lengthy tasks without blocking the loop.
GUI components support the lists of callbacks, called Listeners, which are typically populated when the components are created. EDT executes the listeners when user excitates the components somehow
Timer
For short tasks that must access/modify GUI periodically or at specific time, javax.swing.Timer is used. It can be considered as an invisible GUI component, whose listeners are registered to fire at specific time. Equivalents
System.Windows.Forms.Timer -.NET Framework
flash.utils.Timer - Adobe Flash
Requests from other threads
Other application threads can pass some code to be executed in the event dispatching thread by means of helper classes. The submitted code must be wrapped with a object. Two methods of these classes allow:
synchronous code execution
and asynchronous code execution
from the event dispatching thread. The methodinvokeAndWait should never be called from the event dispatching thread—it will throw an exception. The method or can be called to determine if the current thread is the event dispatching thread. The code supplied by means of the invokeLater and invokeAndWait to the EDT must be as quick as possible to prevent freezing. They are normally intended to deliver the result of a lengthy computation to the GUI.
Both execution of a task in another thread and presenting the results in the EDT can be combined by means of worker design pattern. The javax.swing.SwingWorkerclass, developed by Sun Microsystems, is an implementation of the worker design pattern, and as of Java 6 is part of standard Swing distribution. SwingWorker is normally invoked from EDT-executed event Listener to perform a lengthy task in order not to block the EDT.
Samples
SwingWorker worker = new SwingWorker ; worker.execute;
If you use Groovy and groovy.swing.SwingBuilder, you can use doLater, doOutside, and edt. Then you can write it more simply like this: doOutside
SwingWorker is normally created for a lengthy tasks by EDT while handling callback events. Spawning a worker thread, EDT proceeds handling current message without waiting the worker to complete. Often, your EDT handles a GUI component action, which demands the user to make a choice by means of another dialog, like JFileChooser, which pops up, stays responsive while user picks its option and action proceeds with selected file only after "OK" button is pressed. You see, this takes time and you need a responsive GUI during all this time while EDT is blocking. The vicious cycle is broken through EDT entering a new message loop, which dispatches the messages as per normal until "modal dialog is over" arrives and normal message processing resumes from the blocked position in the component action. The open source project emulates the Swing message loop pumping to provide the "synchronous" execution mechanism for arbitrary user tasks, which proceeds only after the worker completes the task. button.addActionListener );
Since Java 1.7, Java provides standard solution for custom secondary message loops by exposing createSecondaryLoop in system EventQueue.