Skip to content

Instantly share code, notes, and snippets.

@sheva29
Created January 12, 2018 19:49
Show Gist options
  • Save sheva29/f29dbe17340d342c729c2d3bb93cd912 to your computer and use it in GitHub Desktop.
Save sheva29/f29dbe17340d342c729c2d3bb93cd912 to your computer and use it in GitHub Desktop.
Parelelizing Void Method in Java using google Thread Factory
ThreadFactory factory = com.google.appengine.api.ThreadManager.currentRequestThreadFactory();
ExecutorService service = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), 16, 60, TimeUnit.SECONDS, new SynchronousQueue<>(), factory);
List<Future<Void>> futures = new ArrayList<>();
for (String tempStopAccountId : aristoDto.getTempStopHdAccountIds()) {
if (aristoDto.getProductFileDtoMap().get(tempStopAccountId) != null) {
aristoDto.getProductFileDtoMap().get(tempStopAccountId).setTempAccount(true);
}
Callable<Void> callable = () -> {
futures.add(processCancellation(tempStopAccountId));
return null;
};
futures.add(service.submit(callable));
}
service.shutdown();
log.info("Waiting for temp calls to come back");
for (Future<Void> future : futures){
try {
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment