Real Life Examples For CountDownLatch and CyclicBarrier

前端 未结 11 1337
余生分开走
余生分开走 2021-01-30 13:52

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

11条回答
  •  一整个雨季
    2021-01-30 14:28

    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.

提交回复
热议问题