可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
In a hypothetical theater ,
it is called Mutex if only one person allow to watch the play
it is called Semaphore if N number of people allow to watch the play.If anybody leave the Theater during the play then other person can be allowed to watch play.
it is called CountDownLatch if no one allowed to enter until every person vacate the theater.Here each person has freewill to leave the theater.
it is called Cyclicbarrier if theater will not start until every person enter in theater. Here showman can not start the show until all the person enter and grab the seat . Once the play finish same barrier will apply for next show
Here , person is Thread , Play is resource.
回答3:
Real World Example
I can see that all the answers are actually missing a real example. As in how these classes can be used in a software realm
CountDownLatch
A Multithreaded download manager.
The download manager will start multiple threads to download each part of the file simultaneously.(Provided the server supports multiple threads to download). Here each thread will call a countdown method of an instantiated latch. After all the threads have finished execution, the thread associated with the countdown latch will integrate the parts found in the different pieces together into one file
CyclicBarrier
Same scenario as above..But assume the files are downloaded from P2P. Again multiple threads downloading the pieces. But here, suppose that you want the intergity check for the downloaded pieces to be done after a particular time interval. Here cyclic barrier plays an important role. After each time interval, each thread will wait at the barrier so that thread associated with cyclibarrier can do the integrity check. This integrity check can be done multiple times thanks to CyclicBarrier
Please correct me if anything not proper.
回答4:
A CyclicBarrier
is reusable, so it's more like a racing tour where everyone meets at a waypoint before proceeding on the next leg of the tour.
回答5:
Use case 1 Suppose you have split a large job into 10 small task, each one a thread. You have to wait for the 10 tasks' end from that threads before considering the job done.
So the main job initiator thread initializes a CountDownLatch to the number of threads used, it distributes tasks to threads and waits for the latch raises zero with await
method. Each executor thread will invoke countDown
at the end of its task. Finally the main thread will be waken when all threads have finished so it considers the all job is done. This scenario uses the doneSignal
latch describes in the CountDownLatch javadoc.
Use case 2 Suppose you have split a large job into a n * m tasks, distributed over n threads. m corresponds to a matrix row and you have a total to compute for each row. In that case, threads must be synchronized after each task ending so that the total for the row is compute. In that case, a CyclicBarrier
initialized with the number of threads n is used to wait for the end of each row computation (m times in fact).
To compare both, the CountDownLatch
is supposed to be used only 1 time and a CyclicBarrier
can be used as many times as the algorithm requires a synchronization point for a set of threads.
回答6:
Theoretical Difference:
In CountDownLatch, main threads waits for other threads to complete their execution.
In CyclicBarrier, worker threads wait for each other to complete their execution.
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.
Real life example:--
CountDownLatch: Consider a IT world scenario where manager divided modules between development teams (A and B) and he wants to assign it to QA team for testing only when both the teams completes their task.
Here manager thread works as main thread and development team works as worker thread. Manager thread waits for development teams thread to complete their task.
CyclicBarrier: Consider the same IT world scenario where manager divided modules between development teams (A and B). He goes on leave and asked both team to wait for each other to complete their respective task once both are done assign it to QA team for testing.
Here manager thread works as main thread and development team works as worker thread. Development team threads wait for other development team threads after completing their task.
回答7:
CountDownLatch:
If we want all of our threads to do
something + countdown
so that other waiting (for count to reach zero) threads can proceed, we can use countdown latch. All prior threads who actually did the countdown can go on in this situation but there is no guarantee that line processed after latch.countdown() will be after waiting for other threads to reach at latch.countdown()
but it has a guarantee that other waiting threads will only start further after latch.await() has reached zero.
CyclicBarrier:
If we want all our thread to
do something + await at common point + do something
(each await call will decrease wait time for threads to carry on further)
CyclicBarrier functionality can be achieved by CountDownLatch only once by calling latch.countdown() followed by latch.await() by all the threads.
but again you cant reset/reuse the countdownlatch.
Best example where I used CyclicBarrier is to initialize multiple caches (warmed by multiple threads) and then starting further processing, and I wanted to reinitialize other caches again in Sync.
回答8:
A cyclic barrier as the name suggests can be used in cycles.
For ex:
I am a company hr looking for N number of resumes from various job portal feeds.
I have a skillset array containing skills sorted in order of priority.
For ex java,c#,python. I want to find N resumes matching java skillset,
but if I dont find the required no. of resumes, I search again
on the next skillset and so on.
I create a worker each of which scans through the resumes, in
the assigned job feeds. Both workers will start with the primary
skillset search in their job feeds.
After performing the search worker will check if the N resumes
were found. If found, the worker will reset the barrier and
return. Else it will wait for the other worker to complete.
If still N resumes were not found, the search would be
resumed again, on the next skill in the skillset array.
So, search can be called recursively/cyclicly without needing to
create a new cyclic barrier.