No, there is no such guarantee, CountDownLatch
dose not have a fair policy.
However, ReentrantLock
and Condition
have fair policy:
Waiting threads are signalled in FIFO order.
The ordering of lock reacquisition for threads returning from waiting
methods is the same as for threads initially acquiring the lock, which
is in the default case not specified, but for fair locks favors those
threads that have been waiting the longest.
So if you want the waiting threads wake up in fifo order, you can combine CountDownLatch
and Condition
:
Create a ReentrantLock
:
ReentrantLock lock = new ReentrantLock(true);
Condition condition = lock.newCondition();
Let T1 and T2 wait on this condition:
lock.lock();
try {
while (...) {
condition.await();
}
} finally {
lock.unlock();
}
doSomething();
Create a new thread T3 wait on CountDownLatch
and signal T1 & T2:
countDownLatch.await();
lock.lock();
try {
condition.signalAll();
} finally {
lock.unlock();
}