WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Java Concurrency Β· Intermediate Β· question 39 of 100

What is the difference between a CountDownLatch and a CyclicBarrier in Java?

πŸ“• Buy this interview preparation book: 100 Java Concurrency questions & answers β€” PDF + EPUB for $5

Both CountDownLatch and CyclicBarrier are used to manage the synchronization between multiple threads. However, there are some key differences between them.

CountDownLatch is used to ensure that a set of threads have completed their tasks before a main thread continues. In other words, it allows one or more threads to wait until a certain number of tasks have completed before proceeding. The number of tasks is set in the constructor of the CountDownLatch class. Each time a task completes, the countDown() method is called on the CountDownLatch object. Once the count reaches zero, the await() method on the CountDownLatch object returns and the waiting thread can continue.

Here is an example code that demonstrates the use of CountDownLatch:

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {

    public static void main(String[] args) throws InterruptedException {
        int numThreads = 5;
        CountDownLatch latch = new CountDownLatch(numThreads);

        for (int i = 0; i < numThreads; i++) {
            new Thread(() -> {
                // Do some work here...
                latch.countDown();
            }).start();
        }

        // Wait for all threads to complete
        latch.await();
        System.out.println("All threads completed.");
    }
}

On the other hand, CyclicBarrier is used to ensure that a set of threads reach a certain point of execution before continuing. In other words, it allows multiple threads to wait for each other to reach a certain point before proceeding. The number of threads is set in the constructor of the CyclicBarrier class. Each time a thread reaches the barrier, the await() method is called on the CyclicBarrier object. Once the specified number of threads have reached the barrier, the threads are released and can continue.

Here is an example code that demonstrates the use of CyclicBarrier:

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierExample {

    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
        int numThreads = 5;
        CyclicBarrier barrier = new CyclicBarrier(numThreads);

        for (int i = 0; i < numThreads; i++) {
            new Thread(() -> {
                // Do some work here...
                try {
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }

        System.out.println("All threads reached the barrier.");
    }
}

In summary, CountDownLatch is used to ensure that a set of threads have completed their tasks before a main thread continues, while CyclicBarrier is used to ensure that a set of threads reach a certain point of execution before continuing.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview β€” then scores it.
πŸ“ž Practice Java Concurrency β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Java Concurrency questions & answers β€” PDF + EPUB for $5

All 100 Java Concurrency questions Β· All topics