T
- task progress measurement unitV
- result typepublic abstract class BackgroundTask<T,V>
extends java.lang.Object
BackgroundWorker
. WatchDog
thread.
BackgroundTask<Integer, Void> task = new BackgroundTask<Integer, Void>(10, this) {
public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
for (int i = 0; i < 5; i++) {
TimeUnit.SECONDS.sleep(1);
}
return null;
}
};
BackgroundTaskHandler taskHandler = backgroundWorker.handle(task);
taskHandler.execute();
Modifier and Type | Class and Description |
---|---|
static interface |
BackgroundTask.ProgressListener<T,V>
Listener of the task life cycle events, complementary to the tasks own methods:
progress(java.util.List) , done(Object) ,
canceled() . |
static class |
BackgroundTask.ProgressListenerAdapter<T,V> |
Modifier | Constructor and Description |
---|---|
protected |
BackgroundTask(long timeoutSeconds)
Create a task with timeout in default SECONDS unit.
|
protected |
BackgroundTask(long timeoutSeconds,
Screen screen)
Create a task with timeout in default SECONDS unit.
|
protected |
BackgroundTask(long timeout,
java.util.concurrent.TimeUnit timeUnit)
Create a task with timeout.
|
protected |
BackgroundTask(long timeout,
java.util.concurrent.TimeUnit timeUnit,
Screen screen)
Create a task with timeout.
|
Modifier and Type | Method and Description |
---|---|
void |
addProgressListener(BackgroundTask.ProgressListener<T,V> progressListener)
Add additional progress listener.
|
void |
canceled()
Called by the execution environment in UI thread if the task is canceled by
BackgroundTaskHandler.cancel() invocation. |
void |
done(V result)
Called by the execution environment in UI thread when the task is completed.
|
Screen |
getOwnerFrame()
Deprecated.
Use
getOwnerScreen() |
Screen |
getOwnerScreen() |
java.util.Map<java.lang.String,java.lang.Object> |
getParams()
Called by the execution environment in UI thread to prepare some execution parameters.
|
java.util.List<BackgroundTask.ProgressListener<T,V>> |
getProgressListeners()
Additional progress listeners.
|
long |
getTimeoutMilliseconds() |
long |
getTimeoutSeconds() |
boolean |
handleException(java.lang.Exception ex)
Called by the execution environment in UI thread if the task
run(TaskLifeCycle) method raised an
exception. |
boolean |
handleTimeoutException()
Called by the execution environment in UI thread if the task timeout is exceeded.
|
void |
progress(java.util.List<T> changes)
Called by the execution environment in UI thread on progress change.
|
void |
removeProgressListener(BackgroundTask.ProgressListener<T,V> progressListener)
Remove a progress listener.
|
abstract V |
run(TaskLifeCycle<T> taskLifeCycle)
Main method that performs a task.
|
protected BackgroundTask(long timeout, java.util.concurrent.TimeUnit timeUnit, Screen screen)
timeout
- timeouttimeUnit
- timeout time unitscreen
- owner frameprotected BackgroundTask(long timeout, java.util.concurrent.TimeUnit timeUnit)
timeout
- timeouttimeUnit
- timeout time unitprotected BackgroundTask(long timeoutSeconds)
timeoutSeconds
- timeout in secondsprotected BackgroundTask(long timeoutSeconds, Screen screen)
timeoutSeconds
- timeout in secondsscreen
- owner framepublic abstract V run(TaskLifeCycle<T> taskLifeCycle) throws java.lang.Exception
TaskLifeCycle.isInterrupted()
and return if it is trueInterruptedException
- return from the method or don't catch it at alltaskLifeCycle
- lifecycle object that allows the main method to interact with the execution environmentjava.lang.Exception
- exception in working threadpublic void done(V result)
result
- result of execution returned by run(TaskLifeCycle)
methodpublic void canceled()
BackgroundTaskHandler.cancel()
invocation.
public boolean handleTimeoutException()
public boolean handleException(java.lang.Exception ex)
run(TaskLifeCycle)
method raised an
exception.ex
- exceptionpublic void progress(java.util.List<T> changes)
changes
- list of changes since previous invocationpublic java.util.Map<java.lang.String,java.lang.Object> getParams()
run(TaskLifeCycle)
method by calling
TaskLifeCycle.getParams()
.@Nullable @Deprecated public final Screen getOwnerFrame()
getOwnerScreen()
@Nullable public final Screen getOwnerScreen()
public final long getTimeoutMilliseconds()
public final long getTimeoutSeconds()
public final void addProgressListener(BackgroundTask.ProgressListener<T,V> progressListener)
progressListener
- listenerpublic final java.util.List<BackgroundTask.ProgressListener<T,V>> getProgressListeners()
public final void removeProgressListener(BackgroundTask.ProgressListener<T,V> progressListener)
progressListener
- listener