Reputation: 15
For me the difference between CountDownLatch and CyclicBarrier is only that CyclicBarrier provides extra functionalities than CountDownLatch like you can execute a certain task when all threads would reach on a barrier point. You can find no of waiting threads and no of arrival threads in cyclic barrier. So it means we can use CyclicBarrier at all place where CountDownLatch is used. Please correct me if i am wrong. So why CountDownLatch is given in java. Why it is not deprecated if we can perform those functionalities with CyclicBarrier.
Upvotes: 0
Views: 411
Reputation: 8758
CyclicBarier
waits for certain number of threads while CountDownLatch
waits for certain number of events (one thread could call CountDownLatch.countDown()
several times). CountDownLatch
cannot be reused once opened. Also the thread which calls CountDownLatch.countDown()
only signals that it finished some stuff. It doesn't block (and in CyclicBarrier.await()
is blocking method) and could continue to do some other stuff.
From javadoc
A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.
Upvotes: 0