public class DefaultTask extends Object implements Task
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).| Constructor and Description |
|---|
DefaultTask(ThreadService threadService,
EventService eventService)
Creates a new task.
|
| Modifier and Type | Method and Description |
|---|---|
void |
cancel(String reason)
Cancels the operation execution, with the given reason for doing so.
|
void |
finish()
reports that the task is finished - synchronous job
|
Runnable |
getCancelCallBack()
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
|
String |
getCancelReason()
Gets a message describing why the operation was canceled.
|
String |
getName()
Gets the name of the object.
|
long |
getProgressMaximum()
Gets the number of steps the task performs in total.
|
long |
getProgressValue()
Gets the step the task is currently performing.
|
String |
getStatusMessage()
Gets a status message describing what the task is currently doing.
|
boolean |
isCanceled()
Gets whether the operation has been canceled.
|
boolean |
isDone()
Checks whether the task has completed.
|
void |
run(Runnable r)
Starts running the task - asynchronous job
|
void |
setCancelCallBack(Runnable r)
If the task is cancelled (external call to
Cancelable.cancel(String)),
the input runnable argument should be executed by task implementors. |
void |
setName(String name)
Sets the name of the object.
|
void |
setProgressMaximum(long max)
Sets the total number of steps.
|
void |
setProgressValue(long step)
Sets the current step.
|
void |
setStatusMessage(String status)
Sets the status message.
|
void |
start()
reports that the task is started - synchronous job
|
void |
waitFor()
Waits for the task to complete - asynchronous job
|
public DefaultTask(ThreadService threadService, EventService eventService)
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.public void run(Runnable r)
Taskpublic void waitFor()
throws InterruptedException,
ExecutionException
TaskwaitFor in interface TaskInterruptedException - if the task is interrupted.ExecutionException - if the task throws an exception while running.public void start()
Taskpublic void finish()
Taskpublic boolean isDone()
Taskpublic String getStatusMessage()
TaskgetStatusMessage in interface Taskpublic long getProgressValue()
TaskgetProgressValue in interface TaskTask.getProgressMaximum() inclusive.Task.getProgressMaximum()public long getProgressMaximum()
TaskgetProgressMaximum in interface TaskTask.getProgressValue()public void setStatusMessage(String status)
TasksetStatusMessage in interface Taskstatus - The message to set.Task.getStatusMessage()public void setProgressValue(long step)
TasksetProgressValue in interface Taskstep - The step vaule to set.Task.getProgressValue()public void setProgressMaximum(long max)
TasksetProgressMaximum in interface Taskmax - The step count to set.Task.getProgressMaximum()public boolean isCanceled()
CancelableisCanceled in interface Cancelablepublic void cancel(String reason)
Cancelable
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.
cancel in interface Cancelablereason - A message describing why the operation is being canceled.public void setCancelCallBack(Runnable r)
TaskCancelable.cancel(String)),
the input runnable argument should be executed by task implementors.setCancelCallBack in interface Taskr - : should be executed if this task is cancelled through
Cancelable.cancel(String)public Runnable getCancelCallBack()
TaskgetCancelCallBack in interface Taskpublic String getCancelReason()
CancelablegetCancelReason in interface Cancelablepublic String getName()
NamedCopyright © 2009–2023 SciJava. All rights reserved.