- Type Parameters:
T- the type of result thisProgressWorkerproduces.V- the type of intermediate result produced by thisProgressWorker
Executes a task on a background thread, invoking its lifecycle handlers on the UI thread as resolved by
Dispatcher. Instances of this class are not reusable.
Each execution runs on its own thread from a shared pool of daemon threads, created as needed and reaped
when idle, so N simultaneous executions use N threads.
Note that this implementation does NOT coalesce progress reports or intermediate result publishing, but simply pushes
those directly to the onProgress and onPublish handlers on the UI thread.
The onStarted handlers are guaranteed to be called on the UI thread before the background task executes,
and the onDone handlers are guaranteed to be called after the background task completes.
All handler types support multiple handlers, which are called in the order they were added.
There are two ways to use this class:
- Builder-only: pass a task and wire handlers via the builder.
- Handler interfaces: implement one of the handler interfaces (
ProgressWorker.TaskHandler,ProgressWorker.ResultTaskHandler,ProgressWorker.ProgressTaskHandler,ProgressWorker.ProgressResultTaskHandler) to encapsulate both the background task and its handlers in a single class. Handler interface methods are wired automatically and called first. Additional handlers can then be added via the builder and are called after.
On successful completion, handlers are called in this order:
onDone → onSuccess → onResult(T) (result-producing tasks only).
Builder based usage example:
ProgressWorker.builder(this::performTask)
.onStarted(this::displayDialog)
.onDone(this::closeDialog)
.onSuccess(this::handleSuccess)
.onResult(this::handleResult)
.onProgress(this::displayProgress)
.onPublish(this::publishMessage)
.onCancelled(this::displayCancelledMessage)
.onException(this::displayException)
.execute();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceBuilds aProgressWorkerinstance.static interfaceProvides builders for a given task type.static interfaceProvides default handler methods for theProgressWorkerlifecycle.static interfaceExtendsProgressWorker.Handlerwith progress and publish handlers.static interfaceReports progress and publishes intermediate results for a ProgressWorkerstatic interfaceA progress aware background task producing a result.static interfaceAProgressWorker.ProgressResultTaskcombined withProgressWorker.ProgressHandler, for encapsulating a progress-aware, result-producing background task and its handlers in a single class.static interfaceA progress aware background task.static interfaceAProgressWorker.ProgressTaskcombined withProgressWorker.ProgressHandler, for encapsulating a progress-aware background task and its handlers in a single class.static interfaceA background task producing a result.static interfaceAProgressWorker.ResultTaskcombined withProgressWorker.Handler, for encapsulating a result-producing background task and its handlers in a single class.static interfaceA background task.static interfaceAProgressWorker.Taskcombined withProgressWorker.Handler, for encapsulating a background task and its handlers in a single class. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbuilder()booleancancel(boolean mayInterruptIfRunning) Attempts to cancel the background task.booleanbooleandone()voidexecute()Executes this worker, running the task on a background thread and its handlers on the UI thread.get()Waits if necessary for the task to complete, and returns its result.
-
Field Details
-
DEFAULT_MAXIMUM
public static final int DEFAULT_MAXIMUM- See Also:
-
-
Method Details
-
builder
- Returns:
- a
ProgressWorker.BuilderFactory
-
execute
public void execute()Executes this worker, running the task on a background thread and its handlers on the UI thread.Must be called on the UI thread, since the
Dispatcherhandler executor is resolved for the current context at this point (relevant for platforms with a per-context UI thread). -
cancel
public boolean cancel(boolean mayInterruptIfRunning) Attempts to cancel the background task.- Parameters:
mayInterruptIfRunning- true if the thread executing the task should be interrupted- Returns:
- false if the task could not be cancelled, typically because it has already completed
-
cancelled
public boolean cancelled()- Returns:
- true if this task was cancelled before it completed
-
done
public boolean done()- Returns:
- true if this task has completed, whether normally, via an exception or cancellation
-
get
Waits if necessary for the task to complete, and returns its result.- Returns:
- the task result
- Throws:
InterruptedException- if the current thread was interrupted while waitingExecutionException- if the task threw an exceptionCancellationException- if the task was cancelled
-