Reputation: 4208
THere is a loop to borrow Objects from A GenericObjectPool and submit to a Executor in each loop. Then, the caller has to wait for all Exectuors to complete.
Here is present code -
private ImplClass implObject;
private Future future;
for (Iterator iter = anArrayList.iterator(); iter.hasNext();) {
//Gets a GenericObjectPool Object
implObject = (ImplClass) this.getImplPool().borrowObject();
future = getExecutorServices().submit(implObject);
}
// Wait for Exectuor to complete
while (!future.isDone()) {
try {
Thread.sleep(Global.WaitTime());
} catch (InterruptedException iex) {
}
}
But this is wrong as future waits for only last Thread. Should I create an Array of Futures to monitor each executor ?
Upvotes: 0
Views: 154
Reputation: 22456
ExecutorService has a specific method for that: ExecutorService.invokeAll(tasks)
The ExecutorCompletionService mentioned in Ralf's answer can also be helpful - it allows you to process results by the caller thread as they arrive.
Upvotes: 1
Reputation: 7197
How about this?:
List<Callable> tasks = new ArrayList<Callable>();
for (Iterator iter = anArrayList.iterator(); iter.hasNext();) {
//Gets a GenericObjectPool Object
implObject = (ImplClass) this.getImplPool().borrowObject();
tasks.add(implObject);
}
List<Future<Object>> futures = getExecutorServices().invokeAll(tasks);
Upvotes: 1