Skip to content

Instantly share code, notes, and snippets.

@debop
Created January 10, 2013 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debop/4499701 to your computer and use it in GitHub Desktop.
Save debop/4499701 to your computer and use it in GitHub Desktop.
run Runnable in Parallel mode
private static ExecutorService newExecutorService() {
return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
public static void runTasks(int count, final Runnable runnable) {
ExecutorService executor = newExecutorService();
try {
final CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
runnable.run();
latch.countDown();
}
});
}
latch.await();
} catch (InterruptedException e) {
if (log.isErrorEnabled())
log.error("작업 수행 중 예외가 발생했습니다.", e);
throw new RuntimeException(e);
} finally {
executor.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment