zapl is absolutely right! The main thread is waiting for the timeout to get over. If you took a thread dump during the time that the main thread is waiting on the latch, you will see something like:
"main" #1 prio=5 os_prio=31 tid=0x00007fa4be002000 nid=0x1303 waiting on condition [0x000000010b74c000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000076abcb598> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(AbstractQueuedSynchronizer.java:1037)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(AbstractQueuedSynchronizer.java:1328)
at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:277)
From the Javadoc of await
:
If the current count is greater than zero then the current thread
becomes disabled for thread scheduling purposes and lies dormant until
one of three things happen:
- The count reaches zero due to invocations of the countDown method; or
- Some other thread interrupts the current thread; or
- The specified waiting time elapses.
In your case, the await
call returns only because of 3).
In the CountDownLatch
Javadoc, the call to the countDown()
method should have been done 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
}
}