Difference Between CountDownLatch And CyclicBarrier in Java
In spite of the fact that both CountDownLatch and CyclicBarrier are utilized as a synchronization help that permits at least one threads to wait however there are sure contrasts between them. Knowing those contrasts between CountDownLatch and CyclicBarrier in Java will assist you with choosing when one of these utilities will serve you better and obviously it is a decent java inquiry question as well.
CountDownLatch is a thread waiting for multiple threads to finish or calling countDown(). When all threads have called countDown(), the awaiting thread continues to execute.
WORKER-1 finished WORKER-2 finished WORKER-3 finished WORKER-4 finished main has finished
CyclicBarrier is that different threads hang tight for one another(wait for each other)and when all have finished their execution, the result needs to be combined in the parent thread.
Number of parties required to trip the barrier = 3 Sum of product and sum = 0 Is the barrier broken? - false Number of parties waiting at the barrier at this point = 0 Sum of product and sum = 36 Barrier reset successful
Difference between CountDownLatch and CyclicBarrier CountDownLatch CyclicBarrier
CountDownLatch is a construct that a thread looks out for while different threads tally down on the latch until it arrives at zero. A CyclicBarrier is a reusable construct where a gathering of threads stands by together until the entirety of the threads shows up. By then, the barrier is broken and a move can alternatively be made. CountDownLatch keeps up a count of tasks. CyclicBarrier keeps up a count of threads. When we are using a CountDownLatch, you must specify the no. of calls to the countdown() method while creating a CountDownLatch object. When we are using CyclicBarrier, you must specify the no. of threads that should call await() function to trip the barrier. It is initialized to N used to make one thread stand by until N strings have finished some activity, or some activity has been finished N times. If you have a CyclicBarrier initialized to 3 that implies you ought to have in any event 3 strings to call await(). CountDownLatch cannot be reused, when count arrives at zero it can’t be reset. CyclicBarrier can be reused after holding threads are released. In CountDownLatch just the current thread that has an issue throws a special case/exception. In a CyclicBarrier, if a thread experiences an issue (timeout, interruption), the wide range of various threads that have reached await() get a special case/exception. It’s advanceable. It’s not advanceable. If the current thread is interrupted, it will throw InterruptedException. It will not impact other threads. If one thread is interrupted while waiting then all other waiting threads will throw BrokenBarrierException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.