Class ProgressWorker<T,V>

java.lang.Object
is.codion.common.model.worker.ProgressWorker<T,V>
Type Parameters:
T - the type of result this ProgressWorker produces.
V - the type of intermediate result produced by this ProgressWorker

public final class ProgressWorker<T,V> extends Object

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:

On successful completion, handlers are called in this order: onDoneonSuccessonResult(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:
  • Field Details

  • Method Details

    • builder

      public static ProgressWorker.BuilderFactory 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 Dispatcher handler 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 waiting
      ExecutionException - if the task threw an exception
      CancellationException - if the task was cancelled