public class WorkQueue extends Object
Collectiontasks = new LinkedList (); WorkQueue q = new WorkQueue(); for (int i = 0; i < numTasks; ++i) tasks.add(new Runnable() { }); // job to do goes here q.run(tasks);
WorkQueue q = new WorkQueue(); Object taskGroupId = Thread.currentThread(); // a unique id q.registerTaskGroup(taskGroupId, numTasks); for (int i = 0; i < numTasks; ++i) q.add(taskGroupId, new Runnable() { }); // job to do goes here q.await(taskGroupId);In the above example, the current thread is used as the group identifier, which ensures that any other thread executing the same code won't use the same identifier, which could result in either thread returning prematurely before its tasks have finished. However, a shared group identifier can allow multiple threads to add tasks for a common goal, with each being able await until all the tasks are finished.
Modifier and Type | Method and Description |
---|---|
void |
add(Object taskGroupId,
Runnable task)
Adds the provided task to the work queue on behalf of the task group
identifier.
|
int |
availableThreads()
Returns the number of threads that are available to this
WorkQueue for processing the enqueued tasks. |
void |
await(Object taskGroupId)
Waits until all the tasks associated with the group identifier have
finished.
|
boolean |
await(Object taskGroupId,
long timeout,
TimeUnit unit)
Waits until all the tasks associated with the group identifier have
finished.
|
long |
getRemainingTasks(Object taskGroupId)
Returns the number of tasks that need to be completed before the group
associated with the key is complete.
|
static WorkQueue |
getWorkQueue()
Returns the canonical instance of the
WorkQueue to be used in
running concurrent tasks. |
static WorkQueue |
getWorkQueue(int numThreads)
Returns the canonical instance of the
WorkQueue to be used in
running concurrent tasks, ensuring the at least the specified
number of threads are available. |
Object |
registerTaskGroup(int numTasks)
Registers a new task group with the specified number of tasks to execute and
returns a task group identifier to use when registering its tasks.
|
boolean |
registerTaskGroup(Object taskGroupId,
int numTasks)
Registers a new task group with the specified number of tasks to execute,
or returns
false if a task group with the same identifier has
already been registered. |
void |
run(Collection<Runnable> tasks)
Executes the tasks using a thread pool and returns once all tasks have
finished.
|
void |
run(Runnable... tasks)
Executes the tasks using a thread pool and returns once all tasks have
finished.
|
public void add(Object taskGroupId, Runnable task)
run
method,
this method returns immediately without waiting for the task to finish.taskGroupId
- an identifier associated with a set of tasks.task
- a task to runIllegalArgumentException
- if the taskGroupId
is not
currently associated with any active taskGrouppublic void await(Object taskGroupId)
IllegalArgumentException
- if the taskGroupId
is not
currently associated with any active taskGrouppublic boolean await(Object taskGroupId, long timeout, TimeUnit unit)
IllegalArgumentException
- if the taskGroupId
is not
currently associated with any active taskGrouppublic long getRemainingTasks(Object taskGroupId)
taskGroupId
- the key associated with a task grouppublic static WorkQueue getWorkQueue()
WorkQueue
to be used in
running concurrent tasks.public static WorkQueue getWorkQueue(int numThreads)
WorkQueue
to be used in
running concurrent tasks, ensuring the at least the specified
number of threads are available.public Object registerTaskGroup(int numTasks)
numTasks
- the number of tasks that will be eventually run as a part
of this group.public boolean registerTaskGroup(Object taskGroupId, int numTasks)
false
if a task group with the same identifier has
already been registered. This identifier will remain valid in the queue
until await
has been called.taskGroupId
- an identifier to be associated with a group of tasksnumTasks
- the number of tasks that will be eventually run as a part
of this group.public void run(Runnable... tasks)
IllegalStateException
- if interrupted while waiting for the tasks
to finishpublic void run(Collection<Runnable> tasks)
IllegalStateException
- if interrupted while waiting for the tasks
to finishpublic int availableThreads()
WorkQueue
for processing the enqueued tasks.Copyright © 2012. All Rights Reserved.