- Event dispatching thread
The event dispatching thread (EDT) is a background thread used in 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 components to redraw themselves, or input events frominput 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 [This problem is not specific to Java Swing. There is the same issue in mostWidget toolkit s, as for exampleWindows Forms , where theBackgroundWorker class performs the same purpose asSwingWorker in Java.] .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.The method
invokeAndWait()
should never be called from the event dispatching thread—it will throw an exception. The method Javadoc:SE|javax/swing|SwingUtilities|isEventDispatchThread() or Javadoc:SE|java/awt|EventQueue|isEventDispatchThread() 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
class, developed bySwingWorker 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 toSwingWorker
.References
See also
*
Abstract Windowing Toolkit
*Swing (Java)
*SwingWorker
*BackgroundWorker , an equivalent.NET Framework class for "SwingWorker"External links
* (Swing API
Javadoc documentation)
* (AWT APIJavadoc documentation)
*
*
* [http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#EDT The Event-Dispatching Thread]
* [http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#SwingWorker SwingWorker] description from the Swing tutorial
* [http://foxtrot.sourceforge.net/ Foxtrot project home page]
* [http://spin.sourceforge.net/ Spin project home page]
* [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 (earlier version) and Foxtrot
Wikimedia Foundation. 2010.