Created
January 10, 2013 05:32
-
-
Save debop/4499701 to your computer and use it in GitHub Desktop.
run Runnable in Parallel mode
This file contains 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
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