|
Groovy Documentation | |||||||
| FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectorg.jdesktop.beans.AbstractBean
org.jdesktop.swingx.BackgroundWorker
public class BackgroundWorker extends org.jdesktop.beans.AbstractBean
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.
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.
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();
| Nested Class Summary | |
|---|---|
private class |
BackgroundWorker.DelegateWorker
Create a new DelegateWorker. |
| Field Summary | |
|---|---|
private BackgroundWorker.DelegateWorker |
delegate
The SwingWorker delegate. |
private ExecutorService |
executor
If specified, this ExecutorService will be used to run the SwingWorker |
private List |
listeners
List of Background listeners |
private boolean |
running
True if execute() has been called, and the background task has not yet finished |
| Constructor Summary | |
BackgroundWorker()
Creates a new instance of BackgroundWorker |
|
| Method Summary | |
|---|---|
void
|
addBackgroundListener(BackgroundListener listener)
Adds a BackgroundListener |
void
|
execute()
|
private void
|
fireDoInBackgroundEvent()
|
private void
|
fireDoneEvent()
|
private void
|
fireProcessEvent(Object[] data)
|
private void
|
fireStartedEvent()
|
BackgroundListener[]
|
getBackgroundListeners()
|
protected ExecutorService
|
getExecutorService()
Returns the executorService bound property. |
float
|
getProgress()
Returns the progress bound property. |
int
|
getProgressPercent()
Returns the progressPercent bound property. |
boolean
|
isDone()
|
boolean
|
isRunning()
@return true if the BackgroundWorker is currently running, false otherwise |
void
|
publish(Object... chunks)
Causes the given "chunks" of data to be published on the Event Dispatch Thread. |
void
|
removeBackgroundListener(BackgroundListener listener)
Returns an array of all BackgroundListeners registered with this object |
protected void
|
setExecutorService(ExecutorService svc)
Sets the executorService bound property. |
void
|
setProgress(float progress)
Set the progress of this worker based on a value between 0 and 1 where 0 represents 0% and 1 represents 100%. |
void
|
setProgressPercent(int percent)
Set the progress of this worker based on a value between 0 and 100 where 0 represents 0% and 100 represents 100%. |
| Methods inherited from class org.jdesktop.beans.AbstractBean | |
|---|---|
| org.jdesktop.beans.AbstractBean#clone(), org.jdesktop.beans.AbstractBean#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#addPropertyChangeListener(java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#removePropertyChangeListener(java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#getPropertyChangeListeners(java.lang.String), org.jdesktop.beans.AbstractBean#getPropertyChangeListeners(), org.jdesktop.beans.AbstractBean#addVetoableChangeListener(java.lang.String, java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#addVetoableChangeListener(java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#removeVetoableChangeListener(java.lang.String, java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#removeVetoableChangeListener(java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#getVetoableChangeListeners(java.lang.String), org.jdesktop.beans.AbstractBean#getVetoableChangeListeners(), org.jdesktop.beans.AbstractBean#wait(), org.jdesktop.beans.AbstractBean#wait(long), org.jdesktop.beans.AbstractBean#wait(long, int), org.jdesktop.beans.AbstractBean#equals(java.lang.Object), org.jdesktop.beans.AbstractBean#toString(), org.jdesktop.beans.AbstractBean#hashCode(), org.jdesktop.beans.AbstractBean#getClass(), org.jdesktop.beans.AbstractBean#notify(), org.jdesktop.beans.AbstractBean#notifyAll() |
| Methods inherited from class Object | |
|---|---|
| wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
| Field Detail |
|---|
private BackgroundWorker.DelegateWorker delegate
private ExecutorService executor
private List listeners
private boolean running
| Constructor Detail |
|---|
public BackgroundWorker()
| Method Detail |
|---|
public void addBackgroundListener(BackgroundListener listener)
public void execute()
Executes the BackgroundWorker, causing it to begin processing. This method MUST be called on the EDT, and MUST NOT be called if the BackgroundWorker is already running.
Execution follows these steps:
publish method of the BackgroundWorker. Calling the
publish method will in turn cause the
process event handler to be called on the EDT. From
this event handler you are free to update the Swing GUI in any way.
If you need to update the progress state, you may do
so directly from the doInBackground event handler. Updates
to the progress will be set via PropertyChangeEvents on the EDT.
private void fireDoInBackgroundEvent()
private void fireDoneEvent()
private void fireProcessEvent(Object[] data)
private void fireStartedEvent()
public BackgroundListener[] getBackgroundListeners()
protected final ExecutorService getExecutorService()
public final float getProgress()
public final int getProgressPercent()
public boolean isDone()
public final boolean isRunning()
public void publish(Object... chunks)
process event handler to be called. That
event handler is guaranteed to be called on the EDT.
This method MUST be called on the background thread.
public void removeBackgroundListener(BackgroundListener listener)
protected final void setExecutorService(ExecutorService svc)
public final void setProgress(float progress)
public final void setProgressPercent(int percent)
Groovy Documentation