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
TaskEvent
s. May be null, in which case no events are
reported.public void run(Runnable r)
Task
public void waitFor() throws InterruptedException, ExecutionException
Task
waitFor
in interface Task
InterruptedException
- if the task is interrupted.ExecutionException
- if the task throws an exception while running.public void start()
Task
public void finish()
Task
public boolean isDone()
Task
public String getStatusMessage()
Task
getStatusMessage
in interface Task
public long getProgressValue()
Task
getProgressValue
in interface Task
Task.getProgressMaximum()
inclusive.Task.getProgressMaximum()
public long getProgressMaximum()
Task
getProgressMaximum
in interface Task
Task.getProgressValue()
public void setStatusMessage(String status)
Task
setStatusMessage
in interface Task
status
- The message to set.Task.getStatusMessage()
public void setProgressValue(long step)
Task
setProgressValue
in interface Task
step
- The step vaule to set.Task.getProgressValue()
public void setProgressMaximum(long max)
Task
setProgressMaximum
in interface Task
max
- The step count to set.Task.getProgressMaximum()
public boolean isCanceled()
Cancelable
isCanceled
in interface Cancelable
public 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 Cancelable
reason
- A message describing why the operation is being canceled.public void setCancelCallBack(Runnable r)
Task
Cancelable.cancel(String)
),
the input runnable argument should be executed by task implementors.setCancelCallBack
in interface Task
r
- : should be executed if this task is cancelled through
Cancelable.cancel(String)
public Runnable getCancelCallBack()
Task
getCancelCallBack
in interface Task
public String getCancelReason()
Cancelable
getCancelReason
in interface Cancelable
public String getName()
Named
Copyright © 2009–2023 SciJava. All rights reserved.