|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sk.baka.ambient.BackgroundOpExecutor
public final class BackgroundOpExecutor
Able to execute multiple long operations running simultaneously in the
background. Accepts Runnable
task implementations. The tasks will run
in low-priority daemon thread. Thread-safe.
Tasks are differentiated based on a task type object. At most a single
instance of a task of given type may be submitted - multiple submissions of
tasks having the same task type will do nothing unless there is no such task
in executor's queue or already being executed. The task type object must
comply the contract for a Map
key.
A task must periodically check for its interrupt
status. If it is interrupted, it should terminate ASAP. It may do
so by throwing an Exception - exceptions are not reported when the process is
interrupted.
Uncaught exceptions thrown by tasks are reported using
AmbientApplication.error(Class, boolean, String, Throwable)
.
The executor initially starts in offline mode. Use
setOffline(boolean)
to change the mode.
There is no support for mutually exclusive tasks. If you need this functionality you have to implement it yourself, for example by synchronizing on the same object instance.
Constructor Summary | |
---|---|
BackgroundOpExecutor()
|
Method Summary | |
---|---|
void |
backgroundTask(int taskCount,
String name,
int progress,
int maxProgress)
Sets a progress of the current task. |
void |
cancel(Object taskType)
Cancels given task. |
boolean |
isScheduledOrExecuted(Object taskType)
Checks if this task is already scheduled. |
Thread |
newThread(Runnable r)
|
boolean |
schedule(Runnable task,
Object taskType,
boolean online,
String name)
Schedules given task for execution. |
void |
setOffline(boolean offline)
Sets the online/offline state of the executor. |
void |
stopAllTasks()
Immediately stops all running/pending tasks. |
void |
terminate()
Immediately terminates the executor and all pending tasks. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BackgroundOpExecutor()
Method Detail |
---|
public boolean schedule(Runnable task, Object taskType, boolean online, String name)
Schedules given task for execution. Does nothing if the task is rejected to be submitted. Task may be rejected for these reasons:
task
- the task to schedule, must not be null
.taskType
- the task type, must not be null
.online
- if true
then this task processes some online
content.name
- the displayable task name. A grammar construct of name +
"failed"
should be a meaningful text.
true
if the task was scheduled, false
if it was rejected.public boolean isScheduledOrExecuted(Object taskType)
taskType
- the task handle to check, must not be null
.
true
if it is scheduled.public void setOffline(boolean offline)
offline
- if true
then the executor sets itself as offline
and all pending online tasks are terminated.public Thread newThread(Runnable r)
newThread
in interface ThreadFactory
public void stopAllTasks()
public void terminate()
public void backgroundTask(int taskCount, String name, int progress, int maxProgress)
Runnable
.
backgroundTask
in interface IBackgroundTask
taskCount
- unused, ignored.name
- a new name for the task.progress
- current task progress.maxProgress
- maximum progress.public void cancel(Object taskType)
taskType
- the task type, must not be null
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |