Difference between CountDownLatch and CyclicBarrier in Java


Difference between CountDownLatch and CyclicBarrier in Java
Both CyclicBarrier and CountDownLatch are used to implement a scenario where one Thread waits for one or more Thread to complete there job before starts processing but there is one Difference between CountDownLatch and CyclicBarrier in Java which separates them apart and that is, you can not reuse same CountDownLatch instance once count reaches to zero and latch is open, on the other hand CyclicBarrier can be reused by resetting Barrier, Once barrier is broken.


Difference between CountDownLatch and CyclicBarrier in JavaA useful property of a CountDownLatch is that it doesn't require that threads calling countDown wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads could pass.

A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. This barrier action is useful for updating shared-state before any of the parties continue.

The CyclicBarrier uses a fast-fail all-or-none breakage model for failed synchronization attempts: If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads, even those that have not yet resumed from a previous await(), will also leave abnormally via BrokenBarrierException (or InterruptedException if they too were interrupted at about the same time).



9 comments:

  1. In short here are main differences between CyclicBarrier and CountDownLatch concurrency utils in Java :

    1) CyclicBarrier is resulable, CountDownLatch is not.

    2) Both CyclicBarrier and CountDownLatch wait for fixed number of threads.

    3) CountDownLatch is advanceable but CyclicBarrier is not.

    ReplyDelete
  2. How do you write your own CyclicBarrier by using wait and notify methods? This question is asked to my friend, last month on Nomura interview? Can you please write about?

    ReplyDelete
  3. await method implementation is more or less like below
    public synchronized void await()
    throws InterruptedException {
    arrived++;
    if (arrived < N)
    wait();
    else {
    notifyAll();
    arrived = 0;
    }
    }

    ReplyDelete
  4. You just cannot understand difference between cyclic barrier and countdown latch just be reading theory, you need to see the difference and best way is to write code. here is one more example of how you can use CyclicBarrier in Java to stop multiple threads at barrier point with easy to understand explanation.

    ReplyDelete
  5. For those who likes to see an example of how to use CountDownLatch in Java in order to understand the difference in little bit more details, here is one good example with nice explanation.

    ReplyDelete
  6. Its called cyclic barrier because barrier is reusable hence cyclic.

    ReplyDelete
  7. 4th) in CountDownLatch child threads uses parallel execution but in CyclicBarrier its serial execution

    ReplyDelete
  8. Is CycleBarrier is similer with CountDownlatch when CycleBarrier is having only one fixed cycle.If not then which one is to use in one cycle scenario.

    ReplyDelete
    Replies
    1. There is a difference in terms of reuse but if its just one time you have more flexibility. It also depends whether count down situation is more suited to algorithm than reaching to barrier from readability perspective.

      Delete