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.
In CountDownLatch single thread can countdown more than once, this would reduce count by number of times countdown() method is called. In CyclicBarrier single thread can call awaits only once which would reduce barrier count by one only, even if call awaits() method more than once.
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
xxxxxxxxxx
public class CountDownLatchDemo {
// Main driver method
public static void main(String args[])
throws InterruptedException
{
// Let us create task that is going to
// wait for four threads before it starts
CountDownLatch latch = new CountDownLatch(4);
// Creating worker threads
Worker first = new Worker(1000, latch, "WORKER-1");
Worker second = new Worker(2000, latch, "WORKER-2");
Worker third = new Worker(3000, latch, "WORKER-3");
Worker fourth = new Worker(4000, latch, "WORKER-4");
// Starting above 4 threads
first.start();
second.start();
third.start();
fourth.start();
// The main task waits for four threads
latch.await();
// Main thread has started
System.out.println(Thread.currentThread().getName()
+ " has finished");
}
}
// A class to represent threads for which
// the main thread waits.
class Worker extends Thread {
private int delay;
private CountDownLatch latch;
public Worker(int delay, CountDownLatch latch,
String name)
{
super(name);
this.delay = delay;
this.latch = latch;
}
@Override public void run()
{
try {
Thread.sleep(delay);
latch.countDown();
System.out.println(
Thread.currentThread().getName()
+ " finished");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
https://www.geeksforgeeks.org/difference-between-countdownlatch-and-cyclicbarrier-in-java/