Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does CyclicBarrier differ from CountDownLatch, and what happens when one waiting thread is interrupted or times out?

A CyclicBarrier makes a fixed number of threads wait for each other at a common point and releases them all together, then resets itself so it can be used again for the next phase; a CountDownLatch lets threads wait for N events and is one-shot. A CyclicBarrier can run a barrier action once per trip, executed by the last thread to arrive. If any waiting thread is interrupted or times out, the barrier breaks: that thread gets InterruptedException or TimeoutException, and every other waiting thread gets BrokenBarrierException, until reset() is called.

Why this question is asked

Both classes make threads wait, so they are easy to confuse, and the difference matters when we pick one for real code. The second half of the question is where most answers go wrong: a barrier is all-or-nothing, so the way one thread's failure spreads to the others is part of its design, not an accident. Interviewers ask this to see whether we understand that behaviour before we put a barrier into a computation that runs for hours. In this answer, we will recap both classes, compare them side by side, look at the barrier action, and then go through what happens when a thread is interrupted or times out, and what reset() does.

1A Quick Recap of CountDownLatch

A CountDownLatch holds a count. Any thread can call countDown() to decrease it, and any thread can call await() to block until it reaches zero. The threads that count down and the threads that wait are usually different: a main thread waits for three worker threads to finish, for example. Once the count reaches zero, the latch stays open forever, and there is no way to set it back. That is what one-shot means.

Java
CountDownLatch done = new CountDownLatch(3);
for (int i = 0; i < 3; i++) pool.submit(() -> { work(); done.countDown(); });
done.await(); // main thread waits for the three events
System.out.println(done.getCount()); // 0, and it stays 0

2How CyclicBarrier Is Different

A CyclicBarrier is created with a number of parties, the threads that must meet. Each party calls await() when it reaches the meeting point. The first threads to arrive block. When the last party arrives, the barrier trips: all of them are released together. Notice that here the waiting threads and the arriving threads are the same threads; nobody counts down on someone else's behalf.

After tripping, the barrier resets itself automatically, ready for the same parties to meet again. That is the cyclic part. It makes the barrier natural for phased computations, where a group of threads each does part of step 1, waits until all are done, then moves on to step 2.

Java
CyclicBarrier barrier = new CyclicBarrier(3, () -> System.out.println("phase done"));
Runnable worker = () -> {
for (int step = 0; step < 2; step++) {
compute(step);
int index = barrier.await(); // 2, 1 or 0: the arrival index, 0 for the last to arrive
}
};
// three threads run worker; output:
// phase done
// phase done
CountDownLatch and CyclicBarrier compared
QuestionCountDownLatchCyclicBarrier
Who waits?any threads, often a coordinatorthe participating threads themselves
What releases them?count reaches zero via countDown()the last party calls await()
Reusable?no, one-shotyes, resets after each trip
Action on release?noneoptional barrier action, once per trip
One party fails?others wait (until a timeout)barrier breaks, all waiters are released
Typical usewait for N tasks or eventsthreads working in repeated phases

await() returns an arrival index, counting down from parties - 1 to 0, so the thread that gets 0 knows it was the last to arrive. The table below puts the two classes side by side.

3The Barrier Action Runs Once per Trip

The second constructor argument is a Runnable called the barrier action. It runs exactly once each time the barrier trips, after the last party arrives and before any party is released. It runs in the thread of the last party to arrive, so no extra thread is involved.

That timing makes it the right place for work that must happen between phases: merging the partial results of the step, swapping input and output buffers, or writing a checkpoint. All other parties are still blocked while it runs, so it sees a consistent state. But there is a catch: if the barrier action throws an exception, the barrier breaks, the last thread gets that exception from await(), and the other parties get BrokenBarrierException.

4What Happens When One Waiting Thread Is Interrupted or Times Out

This is the all-or-nothing rule. A barrier can only trip if every party arrives, so if one party gives up, the barrier can never trip for that generation. Rather than leave the others waiting forever, CyclicBarrier breaks itself. Concretely, if a waiting thread is interrupted, that thread gets InterruptedException; if a thread used await(timeout, unit) and the time ran out, that thread gets TimeoutException. In both cases the barrier is marked broken and every other thread waiting on it wakes up with BrokenBarrierException.

The broken state also affects threads that have not arrived yet. Any thread that calls await() on a broken barrier gets BrokenBarrierException immediately, without blocking. We can check the state with isBroken().

Java
CyclicBarrier b = new CyclicBarrier(3);
Thread t1 = Thread.ofPlatform().start(() -> awaitAndPrint(b, "t1"));
Thread t2 = Thread.ofPlatform().start(() -> {
try { b.await(100, TimeUnit.MILLISECONDS); }
catch (TimeoutException e) { System.out.println("t2 timed out"); } // t2 timed out
catch (Exception e) { }
});
t1.join(); t2.join();
System.out.println(b.isBroken()); // true
// awaitAndPrint printed for t1: t1 BrokenBarrierException
b.await(); // throws BrokenBarrierException at once
Step throughOne timeout breaks the barrier for everyone
t1Barrier (3 parties)t2
await()
await(100 ms)
TimeoutException after 100 ms
mark broken
BrokenBarrierException
await() again
BrokenBarrierException at once

7 messages in order. Play them, or step through them one at a time.

Notice that only two of the three parties ever arrived. t2 gave up after 100 milliseconds, which broke the barrier, and t1 was released with BrokenBarrierException instead of waiting forever. This is usually what we want: in a phased computation, if one part of a step cannot complete, the step cannot complete, and every participant should find out.

5What reset() Does

reset() puts the barrier back into its initial state, so it can be used again after it broke. If any threads are waiting at the moment reset() is called, they are released with BrokenBarrierException, because from their point of view the generation they were waiting in has been abandoned.

That makes reset() risky to call while parties are still running: some may have arrived in the old generation and be broken out, while others arrive later in the new generation and wait for parties that already left. The safe way is to let all parties stop first, then reset, and start the next attempt deliberately. The Javadoc itself suggests that after a break, it may be simpler to create a new barrier.

6Follow-up questions

Can a CountDownLatch be used like a barrier?

Yes, once. If each of N threads calls countDown() and then await() on a latch of N, they all wait until the last one arrives, which looks like a barrier. But it cannot be reused for the next phase, it has no barrier action, and one thread failing does not release the others, so for repeated phases CyclicBarrier or Phaser fits better.

Which thread runs the barrier action?

The last thread to arrive at the barrier, before any of the parties are released. That is why the action sees every party's work for the step, and why a slow action delays everyone.

What if a thread is interrupted while not waiting at the barrier?

Nothing happens to the barrier until that thread calls await(). If its interrupt status is still set when it does, await() breaks the barrier and throws InterruptedException in that thread, and the others get BrokenBarrierException.

7Conclusion

In this answer, we saw that a CountDownLatch waits for N events once, while a CyclicBarrier makes a fixed group of threads meet, releases them together and resets itself for the next phase. The optional barrier action runs once per trip in the last thread to arrive, which makes it the place for between-phase work. If any waiting party is interrupted or times out, or the action throws, the barrier breaks: that party gets its own exception, and all others get BrokenBarrierException, now or when they next call await(), until reset() restores it.

What to say in the interviewAbout 80 seconds aloud

Open with

A CountDownLatch is a one-shot counter: some threads call countDown, other threads wait until it reaches zero, and once it's open it stays open. A CyclicBarrier is for a fixed group of threads that wait for each other: each party calls await at the meeting point, the last one to arrive trips the barrier, everyone is released together, and the barrier resets itself so the same group can meet again in the next phase.

Then, if they want more

You can also give it a barrier action, which runs once per trip in the last arriving thread before anyone is released, so it's a good place to merge results or checkpoint. The interesting part is failure.

A barrier is all-or-nothing, so if one waiting thread is interrupted, it gets InterruptedException, or if it used a timed await and ran out, it gets TimeoutException, and in both cases the barrier is marked broken and every other waiting thread gets BrokenBarrierException.

Any thread that arrives later gets BrokenBarrierException straight away. reset puts it back to the initial state, but it also breaks any threads currently waiting, so I'd only call it after all parties have stopped, or just create a new barrier.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does CyclicBarrier differ from CountDownLatch, and what happens when one waiting...": https://scrivio-interview-library.netlify.app/java/synchronizers/ec159d3034/