Difference between CountDownLatch and Circular Barrier

Difference between CountDownLatch and Circular Barrier


CountDownLatch Circular Barrier
CountDownLatch Example Circular Barrier Example
A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. The Cyclic Barrier is initialized with a given count and Runnable task which is run once the common barrier condition is met.
CountDownLatch is a one-shot phenomenon -- the count cannot be reset CountDownLatch can be reset - rideBarrier.reset();
When using a CountDownLatch, we specify the number of calls to countDown() that will result in all waiting threads being released. This means that you can use a CountDownLatch with only a single thread.
Difference between CountDownLatch and Circular Barrier
When using a CyclicBarrier, the assumption is that we specify the number of waiting threads that trigger the barrier. If we specify 8, you must have at least 8 threads to call await().
Difference between Circular Barrier and CountDownLatch
In CountDownLatch, main threads waits for other threads to complete their execution. In CyclicBarrier, worker threads wait for each other to complete their execution.