Wednesday, March 10, 2021

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. Knowing those differences between CountDownLatch and CyclicBarrier in Java will help you to decide when one of these utilities will serve you better and of course it is a good java interview question too.

CountDownLatch Vs CyclicBarrier in Java

  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 event occur.

    When you are using CyclicBarrier in Java 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. 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 in Java 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. Java Phaser With Examples
  2. CopyOnWriteArrayList in Java With Examples
  3. Java ReentrantLock With Examples
  4. Executor And ExecutorService in Java With Examples
  5. Java Concurrency Interview Questions And Answers

You may also like-

  1. Deadlock in Java Multi-Threading
  2. Java ThreadLocal Class With Examples
  3. Lambda Expression Examples in Java
  4. PermGen Space Removal in Java 8
  5. Method overriding in Java
  6. Difference Between Abstract Class And Interface in Java
  7. final Vs finally Vs finalize in Java
  8. Introduction to Hadoop Framework

No comments:

Post a Comment