Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaDifference Between CountDownLatch And CyclicBarrier in Java

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

Example: 

Java




// Java Program to demonstrate Usage of CountDownLatch
// Its used when a thread needs to wait for other
// threads before starting its work
 
// Importing required classes
import java.util.concurrent.CountDownLatch;
 
// Main class
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();
        }
    }
}


Output

WORKER-1 finished
WORKER-2 finished
WORKER-3 finished
WORKER-4 finished
main has finished

CyclicBarrier is when 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.

Example

Java




// Java program to demonstrate Execution on Cyclic Barrier
 
// Importing required classes
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
 
// Class 1
// Class implementing Runnable interface
class Computation1 implements Runnable {
 
    public static int product = 0;
    public void run()
    {
        product = 2 * 3;
        try {
            // thread1 awaits for other threads
            Tester.newBarrier.await();
        }
        catch (InterruptedException
               | BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
 
// Class 2
// Implementing Runnable interface
class Computation2 implements Runnable {
 
    public static int sum = 0;
    public void run()
    {
        // check if newBarrier is broken or not
        System.out.println("Is the barrier broken? - "
                           + Tester.newBarrier.isBroken());
        sum = 10 + 20;
        try {
            Tester.newBarrier.await(3000,
                                    TimeUnit.MILLISECONDS);
 
            // number of parties waiting at the barrier
            System.out.println(
                "Number of parties waiting at the barrier "
                + "at this point = "
                + Tester.newBarrier.getNumberWaiting());
        }
        catch (InterruptedException
               | BrokenBarrierException e) {
            e.printStackTrace();
        }
        catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
 
public class Tester implements Runnable {
 
    // create a static CyclicBarrier instance
    public static CyclicBarrier newBarrier
        = new CyclicBarrier(3);
 
    public static void main(String[] args)
    {
        // parent thread
        Tester test = new Tester();
 
        Thread t1 = new Thread(test);
 
        // Starting the thread using start() method
        t1.start();
    }
 
    // Method
    public void run()
    {
        // Print statement
        System.out.println(
            "Number of parties required to trip the barrier = "
            + newBarrier.getParties());
        System.out.println(
            "Sum of product and sum = "
            + (Computation1.product + Computation2.sum));
 
        // Creating object of class 1 objects
        // on which the child thread has to run
        Computation1 comp1 = new Computation1();
        Computation2 comp2 = new Computation2();
 
        // creation of child thread
        Thread t1 = new Thread(comp1);
        Thread t2 = new Thread(comp2);
 
        // Moving child thread to runnable state
        t1.start();
        t2.start();
 
        try {
            // parent thread awaits
            Tester.newBarrier.await();
        }
 
        catch (InterruptedException
               | BrokenBarrierException e) {
 
            // Display exceptions along with line number
            // using printStackTrace() method
            e.printStackTrace();
        }
 
        // barrier breaks as the number of thread waiting
        // for the barrier at this point = 3
        System.out.println(
            "Sum of product and sum = "
            + (Computation1.product + Computation2.sum));
 
        // Resetting the newBarrier
        newBarrier.reset();
        System.out.println("Barrier reset successful");
    }
}


Output

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

Most Popular

Recent Comments