zapl is absolutely right! The main thread is waiting for a timeout. If you took a stream dump while waiting for the main stream on the latch, you will see something like:
"main"
From Javadoc await :
If the current counter is greater than zero, then the current thread is turned off for thread scheduling purposes and is at rest until one of three things happens:
- The count reaches zero due to calls to the countDown method; or
- Some other threads interrupt the current thread; or
- The specified timeout expires.
In your case, the await call returns only because of 3).
In the CountDownLatch Javadoc, the call to the countDown() method must be executed in the finally block:
public void run() { try { startSignal.await(); doWork(); // may throw exception } catch (InterruptedException ex) { // handle interruption ... } finally { doneSignal.countDown(); // cause await to return asap } }
source share