Class DefaultTask
- java.lang.Object
-
- org.scijava.task.DefaultTask
-
- All Implemented Interfaces:
Cancelable,Named,Task
public class DefaultTask extends Object implements Task
Default implementation ofTask. 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 linkedEventService. ATaskEventis 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 callingFuture.cancel(boolean). This default behaviour can be supplemented by an additional custom callback which can be set inTask.setCancelCallBack(Runnable).Synchronous case: A job that reports its status in between calls of
Task.start(), andTask.finish(). It also reports its status via the linkedEventService. Start and finish calls allow publishing properTaskEventto 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 viaTask.setCancelCallBack(Runnable).- Author:
- Curtis Rueden, Nicolas Chiaruttini
-
-
Constructor Summary
Constructors Constructor Description DefaultTask(ThreadService threadService, EventService eventService)Creates a new task.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel(String reason)Cancels the operation execution, with the given reason for doing so.voidfinish()reports that the task is finished - synchronous jobRunnablegetCancelCallBack()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 taskStringgetCancelReason()Gets a message describing why the operation was canceled.StringgetName()Gets the name of the object.longgetProgressMaximum()Gets the number of steps the task performs in total.longgetProgressValue()Gets the step the task is currently performing.StringgetStatusMessage()Gets a status message describing what the task is currently doing.booleanisCanceled()Gets whether the operation has been canceled.booleanisDone()Checks whether the task has completed.voidrun(Runnable r)Starts running the task - asynchronous jobvoidsetCancelCallBack(Runnable r)If the task is cancelled (external call toCancelable.cancel(String)), the input runnable argument should be executed by task implementors.voidsetName(String name)Sets the name of the object.voidsetProgressMaximum(long max)Sets the total number of steps.voidsetProgressValue(long step)Sets the current step.voidsetStatusMessage(String status)Sets the status message.voidstart()reports that the task is started - synchronous jobvoidwaitFor()Waits for the task to complete - asynchronous job
-
-
-
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 asTaskEvents. May be null, in which case no events are reported.
-
-
Method Detail
-
run
public void run(Runnable r)
Description copied from interface:TaskStarts running the task - asynchronous job
-
waitFor
public void waitFor() throws InterruptedException, ExecutionExceptionDescription copied from interface:TaskWaits for the task to complete - asynchronous job- Specified by:
waitForin interfaceTask- Throws:
InterruptedException- if the task is interrupted.ExecutionException- if the task throws an exception while running.
-
start
public void start()
Description copied from interface:Taskreports that the task is started - synchronous job
-
finish
public void finish()
Description copied from interface:Taskreports that the task is finished - synchronous job
-
isDone
public boolean isDone()
Description copied from interface:TaskChecks whether the task has completed.
-
getStatusMessage
public String getStatusMessage()
Description copied from interface:TaskGets a status message describing what the task is currently doing.- Specified by:
getStatusMessagein interfaceTask
-
getProgressValue
public long getProgressValue()
Description copied from interface:TaskGets the step the task is currently performing.- Specified by:
getProgressValuein interfaceTask- Returns:
- A value between 0 and
Task.getProgressMaximum()inclusive. - See Also:
Task.getProgressMaximum()
-
getProgressMaximum
public long getProgressMaximum()
Description copied from interface:TaskGets the number of steps the task performs in total.- Specified by:
getProgressMaximumin interfaceTask- 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:TaskSets the status message. Called by task implementors.- Specified by:
setStatusMessagein interfaceTask- Parameters:
status- The message to set.- See Also:
Task.getStatusMessage()
-
setProgressValue
public void setProgressValue(long step)
Description copied from interface:TaskSets the current step. Called by task implementors.- Specified by:
setProgressValuein interfaceTask- Parameters:
step- The step vaule to set.- See Also:
Task.getProgressValue()
-
setProgressMaximum
public void setProgressMaximum(long max)
Description copied from interface:TaskSets the total number of steps. Called by task implementors.- Specified by:
setProgressMaximumin interfaceTask- Parameters:
max- The step count to set.- See Also:
Task.getProgressMaximum()
-
isCanceled
public boolean isCanceled()
Description copied from interface:CancelableGets whether the operation has been canceled.- Specified by:
isCanceledin interfaceCancelable
-
cancel
public void cancel(String reason)
Description copied from interface:CancelableCancels 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:
cancelin interfaceCancelable- Parameters:
reason- A message describing why the operation is being canceled.
-
setCancelCallBack
public void setCancelCallBack(Runnable r)
Description copied from interface:TaskIf the task is cancelled (external call toCancelable.cancel(String)), the input runnable argument should be executed by task implementors.- Specified by:
setCancelCallBackin interfaceTask- Parameters:
r- : should be executed if this task is cancelled throughCancelable.cancel(String)
-
getCancelCallBack
public Runnable getCancelCallBack()
Description copied from interface:TaskReturns 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:
getCancelCallBackin interfaceTask
-
getCancelReason
public String getCancelReason()
Description copied from interface:CancelableGets a message describing why the operation was canceled.- Specified by:
getCancelReasonin interfaceCancelable- 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:NamedGets the name of the object.
-
-