Friday, October 4, 2013

CyclicBarrier vs CountDownLatch in Java

If you want to get an idea of these two terms before looking into the differences. Please have a look at the following posts. These will explain both the terms with the help of real scenario examples.

CyclicBarrier in Java
CountDownLatch in Java


  • CyclicBarrier as name suggest. CyclicBarrier can be used re-used after the waiting threads are released for synchronization. So the cyclic barrier is used for multiple synchronization points where as CountDownLatch is one synchronization point because we can't re-use it.
  • CyclicBarrier takes an (optional) Runnable task which is run once the common barrier condition is met. where as in CountDownLatch there is no optional runnable task.
  • When using a CyclicBarrier, the assumption is that you specify the number of waiting threads that trigger the barrier. If you specify 3, you must have at least 3 threads to call await(). When using a CountDownLatch, you specify the number of calls to countDown() that will result in all waiting threads being released.
  • In CyclicBarrier  If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads waiting at that barrier point will also leave abnormally via BrokenBarrierException (or InterruptedException if they too were interrupted at about the same time). where as this is not present in CountDownLatch


Related Post
Semaphore in Java
How Thread exchange data in java
Semaphore vs CountDownLatch
How class are loaded in JVM


If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...

No comments:

Post a Comment