Content deleted Content added
No edit summary |
m HTTP to HTTPS for SourceForge |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 3:
The 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 is called the '''primordial worker''' in [[Adobe Flash]] and the '''UI thread''' in [[Standard Widget Toolkit|SWT]], [[.NET Framework]] and [[Android (operating system)|Android]].
== Message
A software application normally consists of multiple threads and a single
| url=http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
| title=The Event Dispatch Thread
Line 12:
| url=http://weblogs.java.net/blog/alexfromsun/archive/2005/11/debugging_swing_1.html
| title=Debugging Swing - is it really difficult?
| publisher=
| accessdate=2011-10-02
| archive-url=https://web.archive.org/web/20110805132240/http://weblogs.java.net/blog/alexfromsun/archive/2005/11/debugging_swing_1.html
Line 32:
There are various solutions for submitting code to the EDT and performing lengthy tasks without blocking the loop.
===Component
GUI components support the lists of callbacks, called Listeners, which are typically populated when the components are created. EDT executes the listeners when user excitates the components somehow (button is clicked, mouse is moved, item is selected, focus is lost, component resized and so on.)
Line 89:
* <code>android.os.AsyncTask</code> - [[Android (operating system)|Android]]
===Modal
SwingWorker is normally created for a lengthy tasks by EDT while handling callback (Listener) events. Spawning a worker thread, EDT proceeds handling current message without waiting the worker to complete. [https://stackoverflow.com/questions/3028842/how-can-swing-dialogs-even-work Often, this is not desirable.]
Often, your EDT handles a GUI component action, which demands the user to make a choice by means of another dialog, like JFileChooser, which pops up, stays responsive while user picks its option and action proceeds with selected file only after "OK" button is pressed. You see, this takes time (user responds in matter of seconds) and you need a responsive GUI (the messages are still pumped in EDT) during all this time while EDT is blocking (it does not handle newer, e.g. JFileChooser, messages in the queue before the dialog is closed and current component action is finished). The vicious cycle is broken through EDT entering a new message loop, which dispatches the messages as per normal until "modal dialog is over" arrives and normal message processing resumes from the blocked position in the component action.
The open source '''[
<syntaxhighlight lang="java">
▲ public void actionPerformed(ActionEvent e)
▲ button.setText("Sleeping...");
▲ text = (String)Worker.post(new Task()
▲ public Object run() throws Exception
}
});
▲ Thread.sleep(10000);
▲ return "Slept !";
▲ catch (Exception x) ...
</syntaxhighlight>
Line 130 ⟶ 124:
* [[Swing (Java)]]
* [[SwingWorker]]
==References==
Line 142 ⟶ 135:
* [http://download.oracle.com/javase/tutorial/uiswing/concurrency/worker.html SwingWorker] description from the Swing tutorial
* [http://tech.stolsvik.com/2009/03/awt-swing-event-pumping-and-targeting.html AWT/Swing event handling] article about event pumping, dispatch and processing, and the EDT
* [
{{DEFAULTSORT:Event Dispatching Thread}}
[[Category:
|