Difference between CountDownLatch vs CyclicBarrier in Java Multithreading

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 their job before starting processing but there is one difference between CountDownLatch and CyclicBarrier in Java which separates them apart and that is, you can not reuse the same CountDownLatch instance once count reaches to zero and latch is open, on the other hand, CyclicBarrier can be reused by resetting Barrier, Once the barrier is broken.

A 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 the 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).

This diagram also nicely explains the difference between CountDownLatch and CyclicBarrier in Java concurrency:

Difference between CountDownLatch and CyclicBarrier in Java


That's all about the difference between CountDownLatch and CyclicBarrier in Java. As I said, the key difference is that you can reuse CyclicBarrier but CountDownLatch cannot be reused once count down reaches zero. 

That's why CyclicBarrier is often used when a task is repeated while CountDownLatch is used for a one-time task like loading the initial cache before start accepting client connections.

Btw, If you are serious about improving your understanding of Java concurrency knowledge and skills, I strongly suggest you should read the Java Concurrency in Practice book by Brian Goetz, Joshua Bloch, Doug Lea, and team, one of the best resources for Java developers.

best book to understand happens before in Java


If you find trouble understanding the book and need someone to explain to you those concepts with more examples, you can also join the Java Concurrency in Practice Bundle course by Heinz Kabutz which is based upon this book and makes it easy to understand those tricky concurrency concepts. It's like someone explaining your Java Concurrency in Practice book live.


best course to learn Java Concurrency in Practice.


Even if you have read the book, going through this course will help you to understand Java Concurrency better and help you to write robust concurrent code with fewer bugs. Sorry, it's almost impossible to write concurrent code without subtle bugs, at least in the first iteration.

The only thing is that some of you may find the course not affordable. There are options to make interest-free part payments and if you can, you should do that. But, if that's not the case then Multithreading and Parallel Computing in Java course on Udemy is also an excellent option to learn Java concurrency better.



Other Java Concurrency Articles you may like
  • The Complete Java Developer RoadMap (roadmap)
  • 5 Courses to Learn Java Multithreading in-depth (courses)
  • 10 Java Multithreading and Concurrency Best Practices (article)
  • Top 50 Multithreading and Concurrency Questions in Java (questions)
  • Top 5 Books to Master Concurrency in Java (books)
  • Difference between Executor, Executors, and ExecutorService in Java (answer)
  • How to avoid deadlock in Java? (answer)
  • Understanding the flow of data and code in Java program (answer)
  • Is Java Concurrency in Practice still valid (answer)
  • How to do inter-thread communication in Java using wait-notify? (answer)
  • 10 Tips to become a better Java Developer (tips)
  • Difference between volatile, synchronized, and atomic variable in Java (answer)
  • How to pause a Thread in Java? (solution)
  • Difference between ForkJoinPool and Executor Framework in Java(answer)
  • How to join two threads in Java? (answer)
  • Difference between Executor and ExecutorService in Java? (answer)
  • What is Happens Before in Java Concurrency? (answer)

Thanks for reading this article so far. If you find my explanation of the difference between CylicBarrier and CountDownLatch useful then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you are a newcomer in Java and want to learn Java Multithreading but looking for a free online training course to start with then I also, suggest you check out this awesome free Java Multithreading course on Udemy. This course is absolutely free and thousands of java developers have already joined this course. 

12 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
    Replies
    1. How do you say CountDownlatch is more advanceable than CyclicBarrier ?

      Delete
  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
  9. It is a "tricky" interview question as the two facilities (barrier and latch) are close in their semantics; in fact, barrier can be replaced for latch if you want to reset the barrier; however, how I differentiate the semantics is thru the analogue that barrier is like a rendezvous point for threads to meet where as latch is like a trip-counter, for all waiting threads to run again.

    ReplyDelete
  10. CountDownLatch : Number of threads will wait till other threads reach a point.

    CyclicBarrier : Number of threads will wait each other to reach a point and continue their execution.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.