Revision: 8607
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at September 30, 2008 12:00 by liqweed
Initial Code
ExecutorService executorService = Executors.newFixedThreadPool(5);
int tasksCount = 10;
final CountDownLatch latch = new CountDownLatch(tasksCount);
for (int i = 0; i < tasksCount; i++ ) {
executorService.execute(new Runnable() {
public void run() {
try {
// Do some work;
} finally {
latch.countDown();
}
}
});
}
try {
latch.await();
} catch (InterruptedException e) {
// todo >> handle exception
}
// All done!
// Do some more work
}
Initial URL
http://java.sun.com/j2se/1.6.0/docs/api/java/util/concurrent/CountDownLatch.html
Initial Description
A simple way to parallelize a for loop in Java. Wrapping each iteration (task) with a Runnable, executing them with an ExecutorService (to pool threads) and coordinating them with a simple CountDownLatch. Due to Java's lack of closures, this simple scenario is rather verbose.
Initial Title
Coordinating Runnable tasks using CountDownLatch
Initial Tags
Initial Language
Java