Event dispatching thread

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 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 [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.] .

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 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 [http://foxtrot.sourceforge.net/ Foxtrot] provides another synchronous execution solution similar to SwingWorker.


See also

* Abstract Windowing Toolkit
* Swing (Java)
* SwingWorker
* BackgroundWorker, an equivalent .NET Framework class for "SwingWorker"

External links

* (Swing API Javadoc documentation)
* (AWT API Javadoc 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.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • SwingWorker — is a popular utility class developed by Sun Microsystems for the Swing library of the Java programming language. Javadoc:SE|javax/swing|SwingWorker enables proper use of the event dispatching thread. As of Java 6,… …   Wikipedia

  • Abstract Window Toolkit — The Abstract Window Toolkit (AWT) is Java s original platform independent windowing, graphics, and user interface widget toolkit. The AWT is now part of the Java Foundation Classes (JFC) mdash; the standard API for providing a graphical user… …   Wikipedia

  • Swing (Java) — Die Widgets von Swing mit dem Ocean Look and Feel (Standard seit Java 1.5) …   Deutsch Wikipedia

  • Model-view-controller — (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the …   Wikipedia

  • Java (programming language) — infobox programming language name = Java paradigm = Object oriented, structured, imperative year = 1995 designer = Sun Microsystems latest release version = Java Standard Edition 6 (1.6.0) latest release date = latest test version = latest test… …   Wikipedia

  • Swing (Java) — Swing is a widget toolkit for Java. It is part of Sun Microsystems Java Foundation Classes (JFC) mdash; an API for providing a graphical user interface (GUI) for Java programs.Swing was developed to provide a more sophisticated set of GUI… …   Wikipedia

  • Java AWT Native Interface — is an interface for the Java programming language that enables rendering libraries compiled to native code to draw directly to a Java Abstract Window Toolkit (AWT) Javadoc:SE|java/awt|Canvas object drawing surface.The Java Native Interface (JNI)… …   Wikipedia

  • Abstract Window Toolkit — Для улучшения этой статьи желательно?: Исправить статью согласно стилистическим правилам Википедии …   Википедия

  • FastPak for Java — is an application loader for Java applications produced by Software and Computer Systems Company, LLC of Reston, Virginia. FastPak for Java uses a single Java virtual machine (JVM) to launch multiple applications, rather than using one JVM per… …   Wikipedia

  • EDT — may refer to: * Eastern Daylight Time, observed in the North American Eastern Time Zone during daylight saving time * Eau de toilette, see perfume * Electrodynamic tether * EDT text editor * Event dispatching thread, a background thread used in… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”