Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real Life Examples For CountDownLatch and CyclicBarrier

One example is given by one of our trainers when he was explaining difference between CountDownLatch and CyclicBarrier.

CountDownLatch: Suppose a stone can be lifted by 10 people so you will wait for all 10 to come. Then only you can lift the stone.

CyclicBarrier: If you are going to a picnic, and you need to first meet at some common point from where you all will start your journey.

If Anybody agrees with these comments please give me some details.

I have already read the sun API for both these classes. But I need some more explaination.

like image 510
Sunny Gupta Avatar asked Apr 14 '12 18:04

Sunny Gupta


People also ask

What is CountDownLatch used for?

CountDownLatch is used to make sure that a task waits for other threads before it starts. To understand its application, let us consider a server where the main task can only start when all the required services have started.

What is the difference between CyclicBarrier and CountDownLatch?

As stated in the definitions, CyclicBarrier allows a number of threads to wait on each other, whereas CountDownLatch allows one or more threads to wait for a number of tasks to complete. In short, CyclicBarrier maintains a count of threads whereas CountDownLatch maintains a count of tasks.

Can CyclicBarrier and CountDownLatch in Java be used?

CountDownLatch and CyclicBarrier both used in multithreading environment and they both are part of. CountDownLatch − A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

Can we reuse CountDownLatch?

tl;dr: the main difference is that unlike a CyclicBarrier , once a CountDownLatch is done and over with, it cannot be reused. The javadoc mentions it explicitly: This is a one-shot phenomenon -- the count cannot be reset.


2 Answers

In a hypothetical theater:

  • It is called Mutex if only one person is allowed to watch the play.
  • It is called Semaphore if N number of people are allowed to watch the play. If anybody leaves the Theater during the play then other person can be allowed to watch the play.
  • It is called CountDownLatch if no one is allowed to enter until every person vacates the theater. Here each person has free will to leave the theater.
  • It is called CyclicBarrier if the play will not start until every person enters the theater. Here a showman can not start the show until all the persons enter and grab the seat. Once the play is finished the same barrier will be applied for next show.

Here, a person is a thread, a play is a resource.

like image 186
Shailesh kumar Avatar answered Oct 12 '22 01:10

Shailesh kumar


The key difference is that CountDownLatch separates threads into waiters and arrivers while all threads using a CyclicBarrier perform both roles.

  • With a latch, the waiters wait for the last arriving thread to arrive, but those arriving threads don't do any waiting themselves.
  • With a barrier, all threads arrive and then wait for the last to arrive.

Your latch example implies that all ten people must wait to lift the stone together. This is not the case. A better real world example would be an exam prompter who waits patiently for each student to hand in their test. Students don't wait once they complete their exams and are free to leave. Once the last student hands in the exam (or the time limit expires), the prompter stops waiting and leaves with the tests.

like image 41
David Harkness Avatar answered Oct 12 '22 03:10

David Harkness