Event dispatching thread: Difference between revisions

Content deleted Content added
Rentar (talk | contribs)
m External links: fix broken external links to Swing Tutorials
Yobot (talk | contribs)
m WP:CHECKWIKI error 61 fixes + general fixes, References after punctuation per WP:REFPUNC and WP:PAIC using AWB (7671)
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>.
 
== 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{{Where|date=July 2010}} 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 [http://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 prevent 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 {{Javadoc:SE|java/lang|Runnable}} object and pass it to the {{Javadoc:SE|javax/swing|SwingUtilities}} helper class or to the {{Javadoc:SE|java/awt|EventQueue}}. Two methods of these classes allow:
* synchronous code execution ({{Javadoc:SE|member=invokeAndWait(Runnable)|javax/swing|SwingUtilities|invokeAndWait(java.lang.Runnable)}} or {{Javadoc:SE|member=invokeAndWait(Runnable)|java/awt|EventQueue|invokeAndWait(java.lang.Runnable)}})
* and asynchronous code execution ({{Javadoc:SE|member=invokeLater(Runnable)|javax/swing|SwingUtilities|invokeLater(java.lang.Runnable)}} or {{Javadoc:SE|member=invokeLater(Runnable)|java/awt|EventQueue|invokeLater(java.lang.Runnable)}})
from the EDT.
 
Line 19:
 
Another solution for executing code in the EDT is using the ''[[SwingWorker|worker design pattern]]''. The <code>[[SwingWorker]]</code> 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 [http://foxtrot.sourceforge.net/ Foxtrot] provides another synchronous execution solution similar to <code>SwingWorker</code>.
 
==References==
{{reflist|2}}
 
== See also ==
Line 28 ⟶ 25:
* [[SwingWorker]]
* [[BackgroundWorker]], an equivalent [[.NET Framework]] class for ''SwingWorker''
 
==References==
{{reflist|2}}
 
== External links ==
Line 40:
* [http://spin.sourceforge.net/ Spin project home page]
 
{{DEFAULTSORT:Event Dispatching Thread}}
[[Category:Java programming language]]