A JavaBean to perform lengthy GUI-interacting tasks in a dedicated thread.
When writing a multi-threaded application using Swing, there are
two constraints to keep in mind:
(refer to
How to Use Threads
for more details):
Time-consuming tasks should not be run on the Event
Dispatch Thread. Otherwise the application becomes unresponsive.
Swing components should be accessed on the Event
Dispatch Thread only.
These constraints mean that a GUI application with time intensive
computing needs at least two threads: 1) a thread to perform the lengthy
task and 2) the Event Dispatch Thread (EDT) for all GUI-related
activities. This involves inter-thread communication which can be
tricky to implement.
BackgroundWorker is a non visual JavaBean that simplifies writing these
multithreaded tasks from within a GUI Builder environment. It is built upon
SwingWorker, and exhibits most of the same API features as SwingWorker.
Workflow
There are two threads involved in the life cycle of a
BackgroundWorker (unlike SwingWorker which can operate with three
threads):
Event Dispatch Thread: All Swing related activities occur
on this thread. BackgroundWorker invokes the
process and done events and notifies
any PropertyChangeListeners on this thread. In addition, the
execute() method must be called on this thread
Worker thread: The doInBackground
event is called on this thread.
This is where all background activities should happen..
A single BackgroundWorker instance may be used more than once. You cannot
however call execute while the BackgroundWorker is in progress.
You can retrieve and set progress information on the BackgroundWorker at
any time. There are two complementary methods for doing so,
setProgress(float) and setProgressPercent(int).
The first method accepts a float between the range of 0 to 1 while the
second method accepts an int between the values of 0 and 100.
You may also specify the exact ExecutorService to use for the background
task. This allows you to specify a thread pool, default thread priority,
and other such attributes of the background task. See the ExecutorService
API for more information.
BackgroundWorker operates on the basis of callbacks. You first register a
BackgroundListener with the BackgroundWorker, and implement the relevant methods.
For example, you implement the doInBackground method if you have
tasks that must be performed on the background thread.
From within the doInBackground method you may call the
publish method of your BackgroundWorker to place some data on
a queue to be processed on the Event Dispatch Thread. At some point the
process event will be fired with this data available within
the BackgroundEvent object. In this event handler you may freely
modify the GUI.
Sample Usage
The following example illustrates the simplest use case. Some
processing is done in the background and when done you update a Swing
component.
Say we want to find the "Meaning of Life" and display the result in
a JLabel .
final JLabel label;
class MeaningOfLifeFinder implements BackgroundListener {
public void doInBackground(BackgroundEvent evt) {
String meaningOfLife = findTheMeaningOfLife();
evt.getWorker().publish(meaningOfLife);
}
public void process(BackgroundEvent evt) {
label.setText("" + evt.getData());
}
public void done(BackgroundEvent evt) {}
public void started(BackgroundEvent evt) {}
}
(new MeaningOfLifeFinder()).execute();
A JavaBean to perform lengthy GUI-interacting tasks in a dedicated thread.
When writing a multi-threaded application using Swing, there are two constraints to keep in mind: (refer to How to Use Threads for more details):
These constraints mean that a GUI application with time intensive computing needs at least two threads: 1) a thread to perform the lengthy task and 2) the Event Dispatch Thread (EDT) for all GUI-related activities. This involves inter-thread communication which can be tricky to implement.
BackgroundWorker is a non visual JavaBean that simplifies writing these multithreaded tasks from within a GUI Builder environment. It is built upon SwingWorker, and exhibits most of the same API features as SwingWorker.
Workflow
There are two threads involved in the life cycle of a BackgroundWorker (unlike SwingWorker which can operate with three threads):
Event Dispatch Thread: All Swing related activities occur on this thread. BackgroundWorker invokes the process and done events and notifies any PropertyChangeListeners on this thread. In addition, the execute() method must be called on this thread
Worker thread: The doInBackground event is called on this thread. This is where all background activities should happen..
A single BackgroundWorker instance may be used more than once. You cannot however call
executewhile the BackgroundWorker is in progress.You can retrieve and set progress information on the BackgroundWorker at any time. There are two complementary methods for doing so,
setProgress(float)andsetProgressPercent(int). The first method accepts a float between the range of 0 to 1 while the second method accepts an int between the values of 0 and 100.You may also specify the exact ExecutorService to use for the background task. This allows you to specify a thread pool, default thread priority, and other such attributes of the background task. See the ExecutorService API for more information.
BackgroundWorker operates on the basis of callbacks. You first register a BackgroundListener with the BackgroundWorker, and implement the relevant methods. For example, you implement the
doInBackgroundmethod if you have tasks that must be performed on the background thread.From within the
doInBackgroundmethod you may call thepublishmethod of your BackgroundWorker to place some data on a queue to be processed on the Event Dispatch Thread. At some point theprocessevent will be fired with this data available within theBackgroundEventobject. In this event handler you may freely modify the GUI.Sample Usage
The following example illustrates the simplest use case. Some processing is done in the background and when done you update a Swing component.
Say we want to find the "Meaning of Life" and display the result in a JLabel .