Java Reference
In-Depth Information
23.11 Multithreading with GUI: SwingWorker
Swing applications present a unique set of challenges for multithreaded programming. All
Swing applications have a single thread, called the event dispatch thread , to handle inter-
actions with the application's GUI components. Typical interactions include updating
GUI components or processing user actions such as mouse clicks. All tasks that require inter-
action with an application's GUI are placed in an event queue and are executed sequentially
by the event dispatch thread.
Swing GUI components are not thread safe—they cannot be manipulated by multiple
threads without the risk of incorrect results that might corrupt the GUI . Unlike the other
examples presented in this chapter, thread safety in GUI applications is achieved not by
synchronizing thread actions, but by ensuring that Swing components are accessed from only
the event dispatch thread . This technique is called thread confinement . Allowing just one
thread to access non-thread-safe objects eliminates the possibility of corruption due to
multiple threads accessing these objects concurrently.
It's acceptable to perform brief calculations on the event dispatch thread in sequence
with GUI component manipulations. If an application must perform a lengthy computa-
tion in response to a user interaction, the event dispatch thread cannot attend to other
tasks in the event queue while the thread is tied up in that computation. This causes the
GUI components to become unresponsive. It's preferable to handle a long-running com-
putation in a separate thread, freeing the event dispatch thread to continue managing
other GUI interactions. Of course, you must update the GUI with the computation's
results from the event dispatch thread, rather than from the worker thread that performed
the computation.
Class SwingWorker
Class SwingWorker (in package javax.swing ) enables you to perform an asynchronous
task in a worker thread (such as a long-running computation) then update Swing compo-
nents from the event dispatch thread based on the task's results. SwingWorker implements
the Runnable interface, meaning that a SwingWorker object can be scheduled to execute in a
separate thread . The SwingWorker class provides several methods to simplify performing a
task in a worker thread and making its results available for display in a GUI. Some com-
mon SwingWorker methods are described in Fig. 23.23.
Method
Description
doInBackground
Defines a long computation and is called in a worker thread.
done
Executes on the event dispatch thread when doInBackground returns.
execute
Schedules the SwingWorker object to be executed in a worker thread.
get
Waits for the computation to complete, then returns the result of the
computation (i.e., the return value of doInBackground ).
Sends intermediate results from the doInBackground method to the pro-
cess method for processing on the event dispatch thread.
publish
Fig. 23.23 | Commonly used SwingWorker methods. (Part 1 of 2.)
 
 
Search WWH ::




Custom Search