Wednesday, 27 January 2016

Difference between CountDownLatch and CyclicBarrier in Java

Though both CountDownLatch and CyclicBarrier are used as a synchronization aid that allows one or more threads to wait but there are certain differences between them that you should know in order to know when one of these utilities will serve you better and of course it is a good java interview question too.

CountDownLatch Vs CyclicBarrier

  1. One of the most important difference is When you are using a CountDownLatch, you specify the number of calls to the countdown() method when creating a CountDownLatch object. So 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.
    What this means is you can use CountDownLatch with only a single thread and using countdown() to decrement as and when the specified even occur.

    When you are using CyclicBarrier you specify the number of threads that should call await() method in order to trip the barrier. That means if you have a CyclicBarrier initialized to 3 that means you should have at least 3 threads to call await().

  2. 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. So with CyclicBarrier you have an option to have an Action class specified in the CyclicBarrier constructor that will be run after the last thread has called await().This barrier action is useful for updating shared-state before any of the parties continue.

    public CyclicBarrier(int parties, Runnable barrierAction)
    

    CountDownLatch doesn't provide any such constructor to specify a runnable action.

  3. CountDownLatch can't be reused, when count reaches zero it cannot be reset.

    CyclicBarrier can be reused after the waiting threads are released.

  4. In a CyclicBarrier, if a thread encounters a problem (timeout, interruption), all the other threads that have reached await() get an exception.

    According to Java docs

    The CyclicBarrier uses an 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 waiting at that barrier point will also leave abnormally via BrokenBarrierException (or InterruptedException if they too were interrupted at about the same time).

    In CountDownLatch only the current thread that has a problem throws exception. According to the description of await() method in CountDownLatch

    If the current thread:

    • has its interrupted status set on entry to this method; or
    • is interrupted while waiting,
    • then InterruptedException is thrown and the current thread's interrupted status is cleared.

That's all for this topic Difference between CountDownLatch and CyclicBarrier in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. CountDownLatch in Java concurrency
  2. CyclicBarrier in Java concurrency
  3. ConcurrentHashMap in Java
  4. CopyOnWriteArrayList in Java
  5. Synchronization in Java multithreading
  6. Java Concurrency interview questions

You may also like -

No comments:

Post a Comment