Package snap.util

Class TaskRunner<T>

java.lang.Object
snap.util.TaskRunner<T>

public class TaskRunner<T> extends Object
A class for running operations in the background.
  • Field Details

  • Constructor Details

    • TaskRunner

      public TaskRunner()
      Constructor.
    • TaskRunner

      public TaskRunner(Callable<T> aSupplier)
      Constructor for given task function.
  • Method Details

    • getName

      public String getName()
      Returns the name of runner (and thread).
    • setName

      public void setName(String aName)
      Sets the name of runner (and thread).
    • getTaskFunction

      public Callable<T> getTaskFunction()
      Returns the task supplier function.
    • setTaskFunction

      public void setTaskFunction(Callable<T> aSupplier)
      Sets the task callable function.
    • getStatus

      public TaskRunner.Status getStatus()
      Returns the status.
    • setStatus

      protected void setStatus(TaskRunner.Status aStatus)
      Sets the status.
    • getMonitor

      public TaskMonitor getMonitor()
      Returns the monitor.
    • setMonitor

      public void setMonitor(TaskMonitor aMonitor)
      Sets the monitor.
    • getThread

      public Thread getThread()
      Returns the thread.
    • isActive

      public boolean isActive()
      Returns whether thread is still active.
    • isCancelled

      public boolean isCancelled()
      Whether runner has been cancelled.
    • start

      public TaskRunner<T> start()
      Starts the runner.
    • cancel

      public void cancel()
      Cancels the runner.
    • terminate

      public void terminate()
      Terminates this runner (clients should try cancel first).
    • run

      public T run() throws Exception
      The method to run.
      Throws:
      Exception
    • success

      protected void success(T aResult)
      This method is called on success.
    • cancelled

      protected void cancelled(Exception e)
      This method is called when cancelled.
    • failure

      protected void failure(Exception e)
      This method is called on failure.
    • finished

      protected void finished()
      This method is called when finished (after success()/failure() call).
    • getResult

      public T getResult()
      Returns the result.
    • awaitResult

      public T awaitResult()
      Waits for this task runner to finish and return the result.
    • join

      public TaskRunner<T> join()
      Joins the runner.
    • join

      public TaskRunner<T> join(int aTimeout)
      Joins the runner.
    • getException

      public Throwable getException()
      Returns the exception.
    • getOnSuccess

      public Consumer<T> getOnSuccess()
      Returns the success handler.
    • setOnSuccess

      public void setOnSuccess(Consumer<T> aHandler)
      Sets the success handler.
    • getOnFailure

      public Consumer<Exception> getOnFailure(Consumer<Exception> aHandler)
      Returns the failure handler.
    • setOnFailure

      public void setOnFailure(Consumer<Exception> aHandler)
      Sets the failure handler.
    • setOnCancelled

      public void setOnCancelled(Runnable aRun)
      Sets the cancelled handler.
    • setOnFinished

      public void setOnFinished(Runnable aRun)
      Sets the finished handler.
    • invokeRun

      protected void invokeRun()
      Runs the run method.
    • invokeFinished

      protected void invokeFinished()
      Runs the success method.
    • addPropChangeListener

      public void addPropChangeListener(PropChangeListener aLsnr)
      Add listener.
    • removePropChangeListener

      public void removePropChangeListener(PropChangeListener aLsnr)
      Remove listener.
    • firePropChange

      protected void firePropChange(String aProp, Object oldVal, Object newVal)
      Fires a property change for given property name, old value, new value and index.