Skip to content

Instantly share code, notes, and snippets.

@cscotta
Created December 14, 2010 18:02
Show Gist options
  • Select an option

  • Save cscotta/740802 to your computer and use it in GitHub Desktop.

Select an option

Save cscotta/740802 to your computer and use it in GitHub Desktop.
// Parallelizes delivery to multiple devices across a worker pool.
// Begin by initializing lists for the jobs and results we're expecting.
// Then, divide the device information across these jobs, and submit them.
// Finally, call ".get()" on each of the jobs to wait until they're done.
@SuppressWarnings("unchecked")
public void parallelizeDelivery() throws ExecutionException {
try {
HashMap[] jobs = new HashMap[numWorkers];
Future[] results = new Future[numWorkers];
for(int i=0; i < numWorkers; i++)
jobs[i] = new HashMap<String, APID.DeliveryInfo>();
int i = 0;
for (Entry entry : deliveryInfo.entrySet()) {
jobs[i % numWorkers].put(entry.getKey(), entry.getValue());
i++;
}
for (int j=0; j < numWorkers; j++)
results[j] = workerPool.submit(new Deliverer(appKey, push, jobs[j]));
for (int j=0; j < numWorkers; j++)
results[j].get();
} catch (InterruptedException e) { logger.warn("Interrupted during delivery.", e); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment