Real Life Examples For CountDownLatch and CyclicBarrier

你离开我真会死。 提交于 2019-12-03 02:20:23

问题


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




回答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

  1. 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

  2. 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.




回答9:


Here are my observations : -----> 1. Where to use what : In a Cyclic barrier threads have to wait for other threads to give some output, and then all threads have to resume processing. So after completing its execution, each thread calls await() method and waits. When the barrier detects that all the threads have reached it, it notifies all its waiting threads and they can resume further execution. Barrier keeps track of count.

In a CountDownLatch single main thread waits for all threads to complete. Each thread reduces the count by 1 after completing execution. After the count reaches 0, the main thread can resume further execution. Main thread keeps track of count.

Phaser : In both ways, count of threads should be known beforehand. It is not possible to add/remove threads dynamically. If the count is not reached, thread keeping track of count will wait infinitely. With the Phaser, the number of threads can be dynamic and vary with time. It is similar to Cyclic Barrier. Thread registers with a barrier. After completing execution, it has two options. It can signal that it has arrived at the barrier point and without waiting for others it can deregister from the Phaser. Second option is that it can wait for the other registered Threads to arrive at the barrier point.

  1. Counts : While creating a CyclicBarrier the no of worker threads includes main thread if it is also going to wait for other threads to complete. While creating a CountDownLatch just needs to mention how many worker threads main thread will to wait to complete. In CountDownLatch there is the concept of main and worker threads and the main waiting thread is not included in count while creating a latch. Phaser can return the current count of registered Threads.

  2. Intent of await() : In CyclicBarrier :: await() all threads including main thread are equal and wait for each other. Hence await() should be given in all threads(worker as well as main thread). CountDownLatch :: await() is given in main thread only and it makes main thread wait till other worker threads make count to 0. Thus internal implementation of both await() is different. There are two concepts in Phaser :: arrival to the barrier(arrive() and arriveAndDeregister()) and waiting(awaitAdvance(phase_number)) for other threads.

  3. Parties and Waiting threads : CountDownLatch cannot give number of waiting threads but can give parties(cl.getCount()), CyclicBarrier can give no of waiting threads cb.getNumberWaiting(), and parties(cb.getParties())

  4. Work responsibilities : Worker thread in countdownlatch need to do countdown(), and await() is done by one single main thread. In cyclicBarrier worker and main threads all do only await() on each other.

  5. Reuse : CyclicBarrier can be reused. cb.await() works for new threads say t1, t2 and main. And second call to cb.await() for new threads t3,t4 and main also works. Main will wait in both calls, that is system is automatically internally resetting the count(or reset()) after barrier is exitted. CountDownLatch cannot be reused. - cl.await() works for new threads say t1, t2 and main. Main thread waits for t1, t2 to complete. But for second cl.await() call for new threads t3,t4 and main will not wait. Phaser object can be re-used again once all the Threads in the set have crossed the barrier point.

  6. After Finish Events : While creating, no finish event can be given in CountDownLatch but it can be given in CyclicBarrier.

class MyClass {

static class MyThread implements Runnable
{

    long waitTime;
    CyclicBarrier cyclicBarrier;
    CountDownLatch countdownlatch;
    Phaser phaser;


    MyThread(  long waitTime, CyclicBarrier cyclicBarrier, CountDownLatch countdownlatch, Phaser phaser){
        this.waitTime = waitTime;
        this.cyclicBarrier = cyclicBarrier;
        this.countdownlatch = countdownlatch;
        this.phaser = phaser;
        this.phaser.register(); //Note additional step here
    }

    @Override
    public void run() {

            try {

                Thread.sleep(waitTime);

                // Diff 4 -----> countdownlatch worker threads need to do countdown and await is done by one single main thread
                //, cyclicBarrier worker threads await on each other
                countdownlatch.countDown(); 
                cyclicBarrier.await();
                phaser.arriveAndAwaitAdvance();

                System.out.println("cyclicBarrier :: " + 
                        ", name :: " + Thread.currentThread().getName() 
                        + ", parties :: " + cyclicBarrier.getParties() 
                        + ", waiting :: "+ cyclicBarrier.getNumberWaiting()); 

                System.out.println("countdownlatch :: " + 
                            "name :: " + Thread.currentThread().getName()  +
                         ", parties :: "+countdownlatch.getCount() +
                         ", waiting :: " + "No method!!" ); 
                System.out.println("phaser :: " + 
                        "name :: " + Thread.currentThread().getName()  +
                     ", parties :: "+phaser.getRegisteredParties() +
                     ", phase :: " + phaser.getPhase()); 

                phaser.arriveAndAwaitAdvance();
                System.out.println("phaser :: " + 
                        "name :: " + Thread.currentThread().getName()  +
                     ", parties :: "+phaser.getRegisteredParties() +
                     ", phase :: " + phaser.getPhase());

                phaser.arriveAndAwaitAdvance();
                System.out.println("phaser :: " + 
                        "name :: " + Thread.currentThread().getName()  +
                     ", parties :: "+phaser.getRegisteredParties() +
                     ", phase :: " + phaser.getPhase());
                phaser.arriveAndDeregister(); 
                if (phaser.isTerminated()) { 
                    System.out.println("Phaser is terminated"); 
                } 

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }

    }

}

public static class MyCBFinishEvent implements Runnable{

    public void run() {

           System.out.println("All threads have reached common barrier point "
                        + ", CyclicBarrrierFinishEvent has been triggered");
           System.out.println("You can update shared variables if any");
    }

}

public static void main(String [] args) throws InterruptedException, BrokenBarrierException{
    //Diff 1 ----- > No finish event can be given in CountDownLatch
    //Diff 5 ------> CyclicBarrier no of worker threads includes main thread, 
    //CountDownLatch is just how many threads, the main waiting thread is not included in count.
    CyclicBarrier cb = new CyclicBarrier(3, new MyCBFinishEvent());
    CountDownLatch cl = new CountDownLatch(2);
    Phaser ph = new Phaser();

    //Diff 2 ----> CountDownLatch cant give num of waiting threads, CyclicBarrier can getNumberWaiting threads
     System.out.println("Start CyclicBarrier - parties :: "+cb.getParties() + ", waiting :: " + cb.getNumberWaiting());
     System.out.println("Start CountDownLatch - parties :: "+cl.getCount() + ", waiting :: " + "No method!!" );

    Runnable t1 = new Thread(new MyThread( 10000, cb, cl, ph));
    Runnable t2 = new Thread(new MyThread( 5000, cb, cl,ph));
     Thread tt1 = new Thread(t1, "t1");
     Thread tt2 = new Thread(t2, "t2");
     tt1.start();
     tt2.start();

     //Diff 6 ---- > await meaning Main waits for t1 and t2 to complete, 
     //CyclicBarrier all are equal. each thread including main thread, if it wants to wait has to do await. 
     //CountDownLatch concept of waiting and workers. main thread await waits till other worker threads make count to 0.
     cb.await();
     cl.await();

     System.out.println("End CyclicBarrier call 1 - parties :: "+cb.getParties() + ", waiting :: " + cb.getNumberWaiting());
     System.out.println("End CountDownLatch call 1 - parties :: "+cl.getCount() + ", waiting :: " + "No method!!" );

     System.out.println("main start created t3, t4 - parties :: "+cl.getCount() + ", waiting :: " + "No method!!" );

     Runnable t3 = new Thread(new MyThread( 6000, cb, cl,ph));
        Runnable t4 = new Thread(new MyThread( 100, cb, cl,ph));
         Thread tt3 = new Thread(t3, "t3");

         Thread tt4 = new Thread(t4, "t4");

         tt3.start();
         tt4.start();

        //Diff -3 -----> 
         //CyclicBarrier - can be reused, main thread waited for t3, t4 to complete.
         //CountDownLatch - for first cl.await(), main waited... second cl.await() call main did not wait!!! 
         cb.await();
         cl.await();


         System.out.println("End main - parties :: "+cb.getParties() + ", waiting :: " + cb.getNumberWaiting());
         System.out.println("end main parties :: "+cl.getCount() + ", waiting :: " + "No method!!" );

}

}



来源:https://stackoverflow.com/questions/10156191/real-life-examples-for-countdownlatch-and-cyclicbarrier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!