Created
December 14, 2010 18:02
-
-
Save cscotta/740802 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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