T
- a type for the key that will represent tracked tasks;
must implement Object#equals
public class TaskDrainer<T> 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, a task may only be finished once, after which it must be started before finishing again. It is okay to finish a task before starting it due to different threads handling starting and finishing.
When draining begins, no more new tasks can be started. This guarantees that at some
point when all the tasks are finished there will be no more collective new tasks,
at which point the TaskDrainer.DrainListener.onDrained()
callback will be invoked.
Modifier and Type | Class and Description |
---|---|
static interface |
TaskDrainer.DrainListener
Fired asynchronously after draining has begun with
beginDrain()
and all tasks that were started have finished. |
Constructor and Description |
---|
TaskDrainer(Handler handler,
TaskDrainer.DrainListener listener)
Create a new task drainer;
onDrained callbacks will be posted to the listener
via the handler . |
TaskDrainer(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 tasks to be started; once all existing started tasks are finished,
fire the
TaskDrainer.DrainListener.onDrained() callback asynchronously. |
void |
taskFinished(T task)
Mark an asynchronous task as having finished.
|
void |
taskStarted(T task)
Mark an asynchronous task as having started.
|
public TaskDrainer(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 TaskDrainer(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(T task)
A task cannot be started more than once without first having finished. Once
draining begins with beginDrain()
, no new tasks can be started.
task
- a key to identify a taskIllegalStateException
- 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(T)
,
beginDrain()
public void taskFinished(T task)
A task cannot be finished more than once without first having started.
task
- a key to identify a taskIllegalStateException
- If attempting to finish a task which is already finished (and not started),taskStarted(T)
,
beginDrain()
public void beginDrain()
TaskDrainer.DrainListener.onDrained()
callback asynchronously.
This operation is idempotent; calling it more than once has no effect.