Event dispatching thread
Encyclopedia
The event dispatching thread (EDT) is a background thread
used in Java
to process events from the Abstract Window Toolkit
(AWT) graphical user interface
event queue. These events are primarily update events that cause user interface components to redraw themselves, or input events from input device
s such as the mouse or keyboard. The AWT uses a single-threaded painting model in which all screen updates must be performed from a single thread. The event dispatching thread is the only valid thread to update the visual state of visible user interface components. Updating visible components from other threads is the source of many common bugs
in Java programs
that use Swing
.
and Swing
object methods are not thread safe: invoking them from multiple threads risks thread interference or memory consistency errors.
To avoid these problems, Swing standards state that all user interface
components should be created and accessed only from the AWT event dispatch thread.
A popular third-party Look and Feel
named Substance goes as far as to refuse to instantiate any Swing component off of the Event Dispatch Thread, to prevent coders from making such a mistake.
from the EDT.
The method
. The method or can be called to determine if the current thread is the event dispatching thread.
Another solution for executing code in the EDT is using the worker design pattern
. The
, is an implementation of the worker design pattern, and as of Java 6 is part of standard Swing distribution. The open source project Foxtrot provides another synchronous execution solution similar to
documentation) (AWT API Javadoc
documentation)
Thread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...
used in Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
to process events from the Abstract Window Toolkit
Abstract Window Toolkit
The Abstract Window Toolkit is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes — the standard API for providing a graphical user interface for a Java program.AWT is also the GUI toolkit for a...
(AWT) graphical user interface
Graphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
event queue. These events are primarily update events that cause user interface components to redraw themselves, or input events from input device
Input device
In computing, an input device is any peripheral used to provide data and control signals to an information processing system such as a computer or other information appliance...
s such as the mouse or keyboard. The AWT uses a single-threaded painting model in which all screen updates must be performed from a single thread. The event dispatching thread is the only valid thread to update the visual state of visible user interface components. Updating visible components from other threads is the source of many common bugs
Software bug
A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program's...
in Java programs
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
that use Swing
Swing (Java)
Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes — an API for providing a graphical user interface for Java programs....
.
Swing and thread safety
Most AWTAbstract Window Toolkit
The Abstract Window Toolkit is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes — the standard API for providing a graphical user interface for a Java program.AWT is also the GUI toolkit for a...
and Swing
Swing (Java)
Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes — an API for providing a graphical user interface for Java programs....
object methods are not thread safe: invoking them from multiple threads risks thread interference or memory consistency errors.
To avoid these problems, Swing standards state that all user interface
Graphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
components should be created and accessed only from the AWT event dispatch thread.
A popular third-party Look and Feel
Pluggable look and feel
Pluggable look and feel is a mechanism used in the Java Swing widget toolkit allowing to change the look and feel of the graphical user interface at runtime....
named Substance goes as far as to refuse to instantiate any Swing component off of the Event Dispatch Thread, to prevent coders from making such a mistake.
Executing code in the EDT
Other application threads can have code executed in the event dispatching thread by defining the code in a object and pass it to the helper class or to the . Two methods of these classes allow:- synchronous code execution ( or )
- and asynchronous code execution ( or )
from the EDT.
The method
invokeAndWait
should never be called from the event dispatching thread—it will throw an exceptionException handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....
. The method or can be called to determine if the current thread is the event dispatching thread.
Another solution for executing code in the EDT is using the worker design pattern
SwingWorker
SwingWorker is a popular utility class developed by Sun Microsystems for the Swing library of the Java programming language. enables proper use of the event dispatching thread...
. The
SwingWorkerSwingWorkerSwingWorker is a popular utility class developed by Sun Microsystems for the Swing library of the Java programming language. enables proper use of the event dispatching thread...
class, developed by Sun MicrosystemsSun Microsystems
Sun Microsystems, Inc. was a company that sold :computers, computer components, :computer software, and :information technology services. Sun was founded on February 24, 1982...
, is an implementation of the worker design pattern, and as of Java 6 is part of standard Swing distribution. The open source project Foxtrot provides another synchronous execution solution similar to
SwingWorker
.See also
- Abstract Window ToolkitAbstract Window ToolkitThe Abstract Window Toolkit is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes — the standard API for providing a graphical user interface for a Java program.AWT is also the GUI toolkit for a...
(AWT) - Swing (Java)Swing (Java)Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes — an API for providing a graphical user interface for Java programs....
- SwingWorkerSwingWorkerSwingWorker is a popular utility class developed by Sun Microsystems for the Swing library of the Java programming language. enables proper use of the event dispatching thread...
- BackgroundWorker, an equivalent .NET Framework.NET FrameworkThe .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
class for SwingWorker
External links
(Swing API JavadocJavadoc
Javadoc is a documentation generator from Sun Microsystems for generating API documentation in HTML format from Java source code.The "doc comments" format used by Javadoc is the de facto industry standard for documenting Java classes. Some IDEs, such as Netbeans and Eclipse automatically generate...
documentation) (AWT API Javadoc
Javadoc
Javadoc is a documentation generator from Sun Microsystems for generating API documentation in HTML format from Java source code.The "doc comments" format used by Javadoc is the de facto industry standard for documenting Java classes. Some IDEs, such as Netbeans and Eclipse automatically generate...
documentation)
- The Event-Dispatching Thread
- SwingWorker description from the Swing tutorial
- AWT/Swing event handling article about event pumping, dispatch and processing, and the EDT
- Foxtrot project home page
- Spin project home page