T - The result typeV - The intermediate results typepublic abstract class SwingTask<T,V> extends Object implements RunnableFuture<T>
SwingTask is very
similar to a SwingWorker: It offers a method
doInBackground() that may be overridden and will be
executed in a background thread, and implements the
RunnableFuture interface.
Additionally, it maintains a ProgressHandler that may be obtained
by calling the getProgressHandler() method, and may be used
by implementors to inform observers of this task about the progress of
the computation. These observers are ProgressListeners that
have been added to this
task, and they will be informed about the progress on the
Event Dispatch Thread.
It also maintains lists of callbacks that may be informed on the
Event Dispatch Thread. These callbacks serve as a
runtime-alternative for overriding the process(List)
and the done() method respectively: The
process callbacks are informed
about intermediate results that are produced by this task and published
via the publish(Object...) method.
The done callbacks are
informed when this task is done().
When any of the callbacks throws an exception, the subsequent
behavior of this task is undefined.
| Modifier | Constructor and Description |
|---|---|
protected |
SwingTask()
Creates a new swing task with a default status message
|
protected |
SwingTask(String message)
Creates a new swing task with the given status message
|
| Modifier and Type | Method and Description |
|---|---|
void |
addDoneCallback(Consumer<SwingTask<T,V>> doneCallback)
Add the given callback to be called on the Event Dispatch Thread,
when this task is
done() |
void |
addProcessCallback(Consumer<List<V>> processCallback)
Add the given callback to be called on the Event Dispatch Thread,
receiving a list of intermediate results that have been passed
to the
publish(Object...) method. |
boolean |
cancel(boolean mayInterruptIfRunning) |
protected abstract T |
doInBackground()
The method that may be overridden by implementors in order
to perform the work in the background thread
|
protected void |
done()
This method is executed on the Event Dispatch Thread after
the
doInBackground() method is finished. |
void |
execute()
Executes this task.
|
T |
get() |
T |
get(long timeout,
TimeUnit unit) |
protected ProgressHandler |
getProgressHandler()
Returns the
ProgressHandler that may be used to inform
this task about the current progress during the execution of
the doInBackground() method. |
boolean |
isCancelled() |
boolean |
isDone() |
protected void |
process(List<V> chunks)
Receives data chunks from the
publish method on the
Event Dispatch Thread. |
protected void |
publish(V... chunks)
Publishes the given data chunks.
|
void |
removeDoneCallback(Consumer<SwingTask<T,V>> doneCallback)
Remove the given callback
|
void |
removeProcessCallback(Consumer<List<V>> processCallback)
Remove the given callback
|
void |
run() |
protected void |
setMessage(String message)
Set the current status message.
|
protected void |
setProgress(double progress)
Set the current progress.
|
protected SwingTask()
protected SwingTask(String message)
message - The status messagepublic final void addProcessCallback(Consumer<List<V>> processCallback)
publish(Object...) method.processCallback - The callback to addpublic final void removeProcessCallback(Consumer<List<V>> processCallback)
processCallback - The callback to removeaddProcessCallback(Consumer)public final void addDoneCallback(Consumer<SwingTask<T,V>> doneCallback)
done()doneCallback - The callback to addpublic final void removeDoneCallback(Consumer<SwingTask<T,V>> doneCallback)
doneCallback - The callback to removeaddDoneCallback(Consumer)protected abstract T doInBackground() throws Exception
Exception - If an exception happens during the computation@SafeVarargs protected final void publish(V... chunks)
doInBackground() method to publish
intermediate results. These will be accumulated and
may be processed in the process(List) method,
which is called on the Event Dispatch Threadchunks - The chunks to publishprotected void process(List<V> chunks)
publish method on the
Event Dispatch Thread. The default implementation is empty
and may be overridden.
Note that instead of overriding this method, it is also possible to
register a process callback
to be informed about intermediate results.
chunks - The intermediate resultspublic final void execute()
public final void run()
run in interface Runnablerun in interface RunnableFuture<T>public final T get() throws InterruptedException, ExecutionException
get in interface Future<T>InterruptedExceptionExecutionExceptionpublic final T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get in interface Future<T>InterruptedExceptionExecutionExceptionTimeoutExceptionpublic final boolean cancel(boolean mayInterruptIfRunning)
public final boolean isCancelled()
isCancelled in interface Future<T>protected void done()
doInBackground() method is finished. The default
implementation is empty and may be overridden.
Note that instead of overriding this method, it is also possible to
register a done callback
to be informed when the work is done.
protected final ProgressHandler getProgressHandler()
ProgressHandler that may be used to inform
this task about the current progress during the execution of
the doInBackground() method.ProgressHandlerprotected final void setMessage(String message)
ProgressListeners will be notified on the Event
Dispatch Threadmessage - The current status messageprotected final void setProgress(double progress)
ProgressListeners will be
notified on the Event Dispatch Threadprogress - The current progressCopyright © 2016. All rights reserved.