My requirement is to wait for two threads to complete execution before kickstarting a dependent job.
In order to do this, I am able to create a CountDownLatch
and a Waiter
Thread
which will wait for the CountDownLatch
to become zero. One constraint is I cannot use the main thread to wait for the two threads to complete. The main thread continues with other tasks.
This thing does work. However, I get a feel of workaround in this than a solid design.
My questions are the following:
- What are obvious flaws in the current approach? eg spurious signal
- What design would you recommend?
My current code:
class Waiter implements Runnable {
private CountDownLatch latch;
Waiter (CountDownLatch latch){
this.latch = latch;
}
@Override
public void run() {
System.out.println("Waiter Started running..." + latch.getCount());
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Waiter ready to trigger Next Job!");
}
}
class Processor implements Runnable {
private CountDownLatch latch;
Processor (CountDownLatch latch){
this.latch = latch;
}
@Override
public void run() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}
public class CountDownLatchDemo {
public static void main (String[] args) throws InterruptedException{
CountDownLatch latch = new CountDownLatch(2);
ExecutorService executor = Executors.newFixedThreadPool(2);
for (int i=0; i< 2; i++){
executor.submit(new Processor(latch));
}
ExecutorService waitExecutor = Executors.newFixedThreadPool(2);
waitExecutor.submit(new Waiter(latch));
Thread.sleep(3000);
executor.shutdown();
waitExecutor.shutdown();
System.out.println("Keep doing other things! Sleep here is just for help you run this code for test!");
}
}