Event dispatching thread: Difference between revisions

Content deleted Content added
Pearle (talk | contribs)
m Changing {{cleanup}} to {{cleanup-date|June 2005}}
A lot of reorganizing and editting--still needs a lot of work
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 Windowing 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 user interface components. Updating visual components from other threads is the source of many common [[Programming bug|bugs]] in Java [[Computer program|programs]] that use [[Swing (Java)|Swing]].
{{cleanup-date|June 2005}}
AWT toolkit in Java uses a single-threaded painting model, in which all screen updates must be performed from a single thread (even in multithreaded applications). Event Dispatch Thread is a background thread used to process events from the AWT event queue, and is the only valid thread to update visual components on the screen. Updating visual components from other threads is the source of most common errors in Swing.
 
== Executing code in the EDT ==
In order to use the Event Dispatch Thread from other application threads, you can wrap your code into a Runnable object, and pass it to [http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html SwingUtilities] helper class. Two methods of this class allow synchronous invokeAndWait(Runnable) and asynchronous (invokeLater(Runnable)) code execution from the EDT. Method invokeAndWait() should never be called from the event dispatch thread - it will throw an Exception in that case. If you are unsure about the current thread, use SwingUtilities.isEventDispatchThread() to check.
 
Other application threads can execute code in the event dispatch 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. Two methods of this class allow synchronous ({{Javadoc:SE|member=invokeAndWait(Runnable)|javax/swing|SwingUtilities|invokeAndWait(java.lang.Runnable)}}) and asynchronous ({{Javadoc:SE|member=invokeLater(Runnable)|javax/swing|SwingUtilities|invokeLater(java.lang.Runnable)}}) code execution from the EDT. The method <code>invokeAndWait()</code> should never be called from the event dispatch thread&mdash;it will throw an [[Exception handling|exception]]. The method{{Javadoc:SE|javax/swing|SwingUtilities|isEventDispatchThread()}} can be called to determine if the current thread is the event dispatching thread.
Another common solution for executing code in the EDT is using the [http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/SwingWorker.java SwingWorker] class. That class was developed by Sun Microsystems, but is not a part of standard Swing distribution.
 
OpensourceAnother solution for executing code in the EDT is using the ''[[Worker pattern|worker design pattern]]''. The <code>[http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/SwingWorker.java SwingWorker]</code> class, developed by [[Sun Microsystems]], is an implementatino of the worker design pattern, but is not a part of standard Swing distribution. The open source project [http://foxtrot.sourceforge.net/ Foxtrot] provides aanother synchronous execution solution similar to <code>SwingWorker</code>.
 
== See also ==
* [[Abstract Windowing Toolkit]]
* [[Swing (Java)]]
 
== External links ==
 
* {{Javadoc:SE|package=javax.swing|javax/swing}} (Swing API [[Javadoc]] documentation)
* {{Javadoc:SE|package=java.awt|java/awt}} (AWT API [[Javadoc] documentation)
* {{Javadoc:SE-guide|swing|Swing API documentation}}
* {{Javadoc:SE-guide|awt|AWT API documentation}}
* [http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#SwingWorkerEDT SwingWorker]The descriptionEvent-Dispatching from java.sun.com tutorial on SwingThread]
* [http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/SwingWorker.java SwingWorker] class source code
* [http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#SwingWorker SwingWorker] description from http://java.sun.com tutorial on Swing
* [http://foxtrot.sourceforge.net/ Foxtrot project home page]
* [http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#SwingWorker SwingWorker] description from java.sun.com tutorial on Swing
* [http://www.swingwiki.org/other:swing_threading Swing multithreading issues] article on SwingWiki.org, containing descriptions and workarounds for several problems caused by EDT misuse
* [http://www.swingwiki.org/best:use_worker_thread_for_long_operations Use worker thread for long operations] article on SwingWiki.org, containing code examples for SwingWorker and Foxtrot
 
[[Category:Java programming language]]