80.154.98.131 (talk) →External links: Removed outdated links. Didn't check all of them |
Hervegirod (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
The '''event dispatching thread''' (EDT) is a background [[Thread (computer science)|thread]] used in [[Java (programming language)|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 [[Software componentry|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 (abstract)|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 [[Software bug|bugs]] in Java [[Computer program|programs]] that use [[Swing (Java)|Swing]] <ref>This problem is not specific to Java [[Swing (Java)|Swing]]. There is the same issue in most [[Widget toolkit]]s, as for example [[Windows Forms]], where the [[BackgroundWorker]] class performs the same purpose as [[SwingWorker]] in Java.</ref>. |
The '''event dispatching thread''' (EDT) is a background [[Thread (computer science)|thread]] used in [[Java (programming language)|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 [[Software componentry|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 (abstract)|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 [[Software bug|bugs]] in Java [[Computer program|programs]] that use [[Swing (Java)|Swing]] <ref>This problem is not specific to Java [[Swing (Java)|Swing]]. There is the same issue in most [[Widget toolkit]]s, as for example [[Windows Forms]], where the [[BackgroundWorker]] class performs the same purpose as [[SwingWorker]] in Java.</ref>. |
||
== Swing and thread safety == |
|||
Most [[Abstract Window Toolkit|AWT]] and [[Swing (Java)|Swing]] object methods are not [[Thread safety|thread safe]]: invoking them from multiple threads risks thread interference or memory consistency errors<ref>{{cite web |
|||
| url=http://java.sun.com/docs/books/tutorial/uiswing/concurrency/dispatch.html |
|||
| title=The Event Dispatch Thread |
|||
| publisher=[[Sun Microsystems]] |
|||
| accessdate=2010-03-19}}</ref><ref>http://weblogs.java.net/blog/alexfromsun/archive/2005/11/debugging_swing_1.html</ref>. To avoid these problems, Swing standards states that all [[Graphical user interface|user interface]] components should be created '''and''' accessed '''only''' from the AWT event dispatch thread. A popular third-party [[Pluggable look and feel|Look and Feel]] named [https://substance.dev.java.net/ Substance] goes as far as to refuse to instantiate any Swing component off of the Event Dispatch Thread<ref>http://www.pushing-pixels.org/?p=368</ref>, to hinder coders from making such a mistake. |
|||
== Executing code in the EDT == |
== Executing code in the EDT == |
||
Line 13: | Line 21: | ||
==References== |
==References== |
||
{{reflist|2}} |
|||
<References/> |
|||
== See also == |
== See also == |
Revision as of 23:12, 19 March 2010
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 devices 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 [1].
Swing and thread safety
Most AWT and Swing object methods are not thread safe: invoking them from multiple threads risks thread interference or memory consistency errors[2][3]. To avoid these problems, Swing standards states 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[4], to hinder coders from making such a mistake.
Executing code in the EDT
Other application threads can execute code in the event dispatching thread by defining the code in a Runnable
object and pass it to the SwingUtilities
helper class or to the EventQueue
. Two methods of these classes allow:
- synchronous code execution (
SwingUtilities.invokeAndWait(Runnable)
orEventQueue.invokeAndWait(Runnable)
) - and asynchronous code execution (
SwingUtilities.invokeLater(Runnable)
orEventQueue.invokeLater(Runnable)
)
from the EDT.
The method invokeAndWait()
should never be called from the event dispatching thread—it will throw an exception. The method SwingUtilities.isEventDispatchThread()
or EventQueue.isDispatchThread()
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 SwingWorker
class, developed by Sun Microsystems, 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
.
References
- ^ This problem is not specific to Java Swing. There is the same issue in most Widget toolkits, as for example Windows Forms, where the BackgroundWorker class performs the same purpose as SwingWorker in Java.
- ^ "The Event Dispatch Thread". Sun Microsystems. Retrieved 2010-03-19.
- ^ http://weblogs.java.net/blog/alexfromsun/archive/2005/11/debugging_swing_1.html
- ^ http://www.pushing-pixels.org/?p=368
See also
- Abstract Window Toolkit (AWT)
- Swing (Java)
- SwingWorker
- BackgroundWorker, an equivalent .NET Framework class for SwingWorker
External links
javax.swing
(Swing API Javadoc documentation)java.awt
(AWT API Javadoc documentation)- Swing API documentation
- AWT API 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