Class DefaultTask

  • All Implemented Interfaces:
    Cancelable, Named, Task

    public class DefaultTask
    extends Object
    implements Task
    Default implementation of Task. Throughout the task (or job), Task.setProgressValue(long) can be called to inform how the job is progressing.

    Asynchronous case: A job (runnable) is sent for execution to the linked ThreadService. It reports status updates via the linked EventService. A TaskEvent is sent before the job is started and when finished. In the asynchronous case, upon task cancellation (Cancelable.cancel(String) call), the runnable associated to the ThreadService is attempted to be stopped by calling Future.cancel(boolean). This default behaviour can be supplemented by an additional custom callback which can be set in Task.setCancelCallBack(Runnable).

    Synchronous case: A job that reports its status in between calls of Task.start(), and Task.finish(). It also reports its status via the linked EventService. Start and finish calls allow publishing proper TaskEvent to subscribers (with the EventService). Upon cancellation of a synchronous task, it is the responsibility of the synchronous task to handle its own cancellation through a custom callback which can be set via Task.setCancelCallBack(Runnable).

    Author:
    Curtis Rueden, Nicolas Chiaruttini
    • Constructor Detail

      • DefaultTask

        public DefaultTask​(ThreadService threadService,
                           EventService eventService)
        Creates a new task.
        Parameters:
        threadService - Service to use for launching the task in its own thread. Required.
        eventService - Service to use for reporting status updates as TaskEvents. May be null, in which case no events are reported.
    • Method Detail

      • run

        public void run​(Runnable r)
        Description copied from interface: Task
        Starts running the task - asynchronous job
        Specified by:
        run in interface Task
      • start

        public void start()
        Description copied from interface: Task
        reports that the task is started - synchronous job
        Specified by:
        start in interface Task
      • finish

        public void finish()
        Description copied from interface: Task
        reports that the task is finished - synchronous job
        Specified by:
        finish in interface Task
      • isDone

        public boolean isDone()
        Description copied from interface: Task
        Checks whether the task has completed.
        Specified by:
        isDone in interface Task
      • getStatusMessage

        public String getStatusMessage()
        Description copied from interface: Task
        Gets a status message describing what the task is currently doing.
        Specified by:
        getStatusMessage in interface Task
      • getProgressMaximum

        public long getProgressMaximum()
        Description copied from interface: Task
        Gets the number of steps the task performs in total.
        Specified by:
        getProgressMaximum in interface Task
        Returns:
        Total number of steps the task will perform, or 0 if unknown.
        See Also:
        Task.getProgressValue()
      • setStatusMessage

        public void setStatusMessage​(String status)
        Description copied from interface: Task
        Sets the status message. Called by task implementors.
        Specified by:
        setStatusMessage in interface Task
        Parameters:
        status - The message to set.
        See Also:
        Task.getStatusMessage()
      • setProgressValue

        public void setProgressValue​(long step)
        Description copied from interface: Task
        Sets the current step. Called by task implementors.
        Specified by:
        setProgressValue in interface Task
        Parameters:
        step - The step vaule to set.
        See Also:
        Task.getProgressValue()
      • setProgressMaximum

        public void setProgressMaximum​(long max)
        Description copied from interface: Task
        Sets the total number of steps. Called by task implementors.
        Specified by:
        setProgressMaximum in interface Task
        Parameters:
        max - The step count to set.
        See Also:
        Task.getProgressMaximum()
      • isCanceled

        public boolean isCanceled()
        Description copied from interface: Cancelable
        Gets whether the operation has been canceled.
        Specified by:
        isCanceled in interface Cancelable
      • cancel

        public void cancel​(String reason)
        Description copied from interface: Cancelable
        Cancels the operation execution, with the given reason for doing so.

        This method merely sets the operation status to canceled; it cannot necessarily stop the operation itself. That is, it is the responsibility of each individual operation to check Cancelable.isCanceled() in a timely manner during execution, and stop doing whatever it is doing if the flag has been tripped.

        Specified by:
        cancel in interface Cancelable
        Parameters:
        reason - A message describing why the operation is being canceled.
      • getCancelCallBack

        public Runnable getCancelCallBack()
        Description copied from interface: Task
        Returns the current cancel callback runnable, This can be used to concatenate callbacks in order, for instance, to ask for a user confirmation before cancelling the task
        Specified by:
        getCancelCallBack in interface Task
      • getCancelReason

        public String getCancelReason()
        Description copied from interface: Cancelable
        Gets a message describing why the operation was canceled.
        Specified by:
        getCancelReason in interface Cancelable
        Returns:
        The reason for cancelation, which may be null if no reason was given, or if the operation was not in fact canceled.
      • getName

        public String getName()
        Description copied from interface: Named
        Gets the name of the object.
        Specified by:
        getName in interface Named
      • setName

        public void setName​(String name)
        Description copied from interface: Named
        Sets the name of the object.
        Specified by:
        setName in interface Named