public class TaskSingleDrainer extends Object
The initial state is to allow all tasks to be started and finished. A task may only be started once, after which it must be finished before starting again. Likewise, finishing a task that hasn't been started is also not allowed.
When draining begins, the task cannot be started again. This guarantees that at some
point the task will be finished forever, at which point the TaskDrainer.DrainListener.onDrained()
callback will be invoked.
Constructor and Description |
---|
TaskSingleDrainer(Handler handler,
TaskDrainer.DrainListener listener)
Create a new task drainer;
onDrained callbacks will be posted to the listener
via the handler . |
TaskSingleDrainer(Handler handler,
TaskDrainer.DrainListener listener,
String name)
Create a new task drainer;
onDrained callbacks will be posted to the listener
via the handler . |
Modifier and Type | Method and Description |
---|---|
void |
beginDrain()
Do not allow any more task re-starts; once the existing task is finished,
fire the
TaskDrainer.DrainListener.onDrained() callback asynchronously. |
void |
taskFinished()
Mark this asynchronous task as having finished.
|
void |
taskStarted()
Mark this asynchronous task as having started.
|
public TaskSingleDrainer(Handler handler, TaskDrainer.DrainListener listener)
onDrained
callbacks will be posted to the listener
via the handler
.handler
- a non-null
handler to use to post runnables tolistener
- a non-null
listener where onDrained
will be calledpublic TaskSingleDrainer(Handler handler, TaskDrainer.DrainListener listener, String name)
onDrained
callbacks will be posted to the listener
via the handler
.handler
- a non-null
handler to use to post runnables tolistener
- a non-null
listener where onDrained
will be calledname
- an optional name used for debug loggingpublic void taskStarted()
The task cannot be started more than once without first having finished. Once
draining begins with beginDrain()
, no new tasks can be started.
IllegalStateException
- If attempting to start a task which is already started (and not finished),
or if attempting to start a task after draining has begun.taskFinished()
,
beginDrain()
public void beginDrain()
TaskDrainer.DrainListener.onDrained()
callback asynchronously.
This operation is idempotent; calling it more than once has no effect.
public void taskFinished()
The task cannot be finished if it hasn't started. Once finished, a task cannot be finished again (unless it's started again).
IllegalStateException
- If attempting to start a task which is already finished (and not re-started),taskStarted()
,
beginDrain()