to do goes here q.run(tasks);
Alternately, a use may register a task group identifier and then iteratively add new tasks associated with that identifier. At some point in the future, the user can then wait for all the tasks associated with that identifier to finish. This second method allows for the iterative construction of tasks, or for cases where not all of the data for the tasks is availabe at once (although the number of tasks is known).
WorkQueue q = WorkQueue.getWorkQueue(); 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.
@author David Jurgens