Quick revision
The short answer
The broken barrier is doing its job: if one slice of a step cannot be computed, the step cannot complete, so all eight workers should stop together. What is missing is the rest of the design: the failing worker should catch and record the original exception and let the others exit cleanly, the barrier action should checkpoint the grid after every completed step so a run can resume, and a coordinator should decide from the error type whether to retry the step (with reset() once all workers have stopped) or abort. If the number of workers must change during the run, a Phaser fits better than a fixed barrier.
What to remember
In this answer, we saw that the broken barrier is the correct signal that a step cannot complete, so all workers stopping together is right; the lost run comes from the missing recovery design. We added four pieces: the failing worker records the original exception and breaks the barrier while the others exit quietly, the barrier action checkpoints the consistent grid every N steps, a coordinator decides from the error type whether to retry from the checkpoint with reset() or a new barrier once all workers have stopped, and a Phaser replaces the barrier if the number of workers must change.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The broken barrier is doing its job: if one slice of a step cannot be computed, the step cannot complete, so all eight workers should stop together. What is missing is the rest of the design: the failing worker should catch and record the original exception and let the others exit cleanly, the barrier action should checkpoint the grid after every completed step so a run can resume, and a coordinator should decide from the error type whether to retry the step (with reset() once all workers have stopped) or abort. If the number of workers must change during the run, a Phaser fits better than a fixed barrier.
Why this question is asked
Long-running parallel computations, such as simulations, model training or large batch transforms, often use a barrier per step, and they fail in exactly this way: hours of work lost because one thread hit one bad cell. Interviewers ask this scenario to see whether we can separate two things: the barrier's correct behaviour, and the recovery design around it that was never built. In this answer, we will recap how the barrier breaks, see why losing the whole run is the real bug, and then add four pieces: recording the original failure, checkpointing in the barrier action, deciding between retry and abort, and when to switch to a Phaser.
1A Quick Recap of How the Barrier Breaks
A CyclicBarrier with eight parties releases the workers only when all eight call await(). If one of them is interrupted, times out or the barrier action throws, the barrier marks itself broken, and every thread waiting on it, or arriving later, gets BrokenBarrierException. It stays broken until someone calls reset().
In our simulation, each worker owns one slice of the grid. In every step it computes the new values of its slice from the previous step's grid, then waits at the barrier, so no worker starts step n + 1 until all slices of step n are written. That rule is what keeps the simulation correct: a worker reading a neighbour's border cells must see the neighbour's finished values.
2Why the Lost Run Is the Real Bug
Stopping every worker when one fails is correct. If worker 3 cannot compute its slice of step 412, there is no valid grid for step 412, and letting the other seven continue would produce a result built on a hole. So the broken barrier is the right signal.
The real bugs are around it. The workers that receive BrokenBarrierException do not know why the barrier broke, so the original exception is often lost or buried in a log. Nothing was saved along the way, so the only way to recover is to start again from step 0. And nobody decides whether the failure was worth retrying. Note also a subtle point: if worker 3's exception simply escapes its run() method, it never calls await(), and the other seven would wait forever unless something breaks the barrier; usually the break comes from a timed await or from a coordinator interrupting the threads.
6 messages in order. Play them, or step through them one at a time.
3Record the Original Failure and Exit Cleanly
The first change is to catch the exception in the worker that failed, record it in a place the coordinator can read, and then break the barrier so the others stop promptly. The others catch BrokenBarrierException, recognise it as a stop signal rather than an error of their own, and return. A shared AtomicReference keeps the first failure, which is the one that matters.
Notice the last two lines of the catch block. There is no public method to break a CyclicBarrier, but await() checks the interrupt status first, so an interrupted await() breaks the barrier immediately and the other seven are released. A coordinator that cancels all worker futures with cancel(true) achieves the same thing from outside. Either way, the coordinator ends up with the original exception, not seven copies of BrokenBarrierException.
4Checkpoint in the Barrier Action
The barrier action runs once per trip, in the last thread to arrive, while every other worker is still blocked. At that moment the grid for the step is complete and nothing is changing it, which makes it the ideal place to save a checkpoint.
How often to checkpoint is a cost trade-off. If a step takes 30 seconds and a checkpoint takes 5 seconds, saving every step adds about 17 percent to the run time, while saving every 50 steps adds about 0.3 percent and risks at most 25 minutes of recomputation. Writing checkpoints to a new file and renaming it at the end keeps a crash during the save from corrupting the previous one.
5Retry or Abort, Based on the Error Type
Once all workers have exited, the coordinator looks at the recorded failure and decides. A transient error, such as an I/O failure reading input or an OutOfMemoryError caused by a temporary spike, is worth a bounded retry from the last checkpoint. A deterministic error, such as a numerical instability or a bug that throws at the same cell every time, will fail again identically, so retrying only wastes time; the run should abort with the cause and the step number.
For a retry, the coordinator calls barrier.reset() or simply creates a new barrier, reloads the checkpoint and starts the workers again. Calling reset() only after every worker has stopped is the important part: resetting while some threads are still waiting would break them out of the old generation while others start a new one, and the group would fall out of step.
- All workers stoppedthe first cause is in failure
- Is the error transient?I/O, timeout, resource spikeYesRetryNoAbort with cause and step number
- Retries left?for example at most 3Yesreset() or new barrier, load last checkpointNoAbort
- Restart 8 workers from checkpoint step + 1same slices, same barrier rules
4 steps in order. Play them, or step through them one at a time.
6When a Phaser Fits Better
A CyclicBarrier has a fixed number of parties for its whole life. If the run should continue with fewer workers after one fails, for example by giving the failed slice to a spare worker or by rebalancing slices across seven workers, a Phaser is the better tool. Workers can register() and arriveAndDeregister() between phases, and onAdvance() plays the role of the barrier action, including deciding to terminate. For a fixed eight workers with checkpoints, though, the barrier with the changes above is simpler and perfectly adequate.
7Follow-up questions
Why not let each worker retry its own slice and keep the barrier intact?
For a transient error inside one slice, a local retry before await() is a good first line of defence, and it can save many full restarts. But it needs a bound, and when the bound is reached the worker must still break the barrier, because the step cannot complete. The coordinator-level retry from a checkpoint remains the safety net.
What if the barrier action itself fails while saving a checkpoint?
Then the barrier breaks, the last thread gets the action's exception from await(), and the others get BrokenBarrierException. That is acceptable, because a failed checkpoint is a failure the coordinator should see. Writing to a temporary file and renaming it keeps the previous checkpoint usable.
Would a ForkJoinPool or parallel stream be simpler?
For a step-by-step grid computation, a common alternative is to run each step as a set of tasks with invokeAll() and join them before the next step. That removes the barrier entirely and makes failures surface as exceptions from the join. It is often simpler, at the cost of creating tasks every step instead of keeping long-lived workers.
8Conclusion
In this answer, we saw that the broken barrier is the correct signal that a step cannot complete, so all workers stopping together is right; the lost run comes from the missing recovery design. We added four pieces: the failing worker records the original exception and breaks the barrier while the others exit quietly, the barrier action checkpoints the consistent grid every N steps, a coordinator decides from the error type whether to retry from the checkpoint with reset() or a new barrier once all workers have stopped, and a Phaser replaces the barrier if the number of workers must change.
Open with
First, I'd say the barrier is behaving correctly. If one slice of step 412 can't be computed, there's no valid grid for that step, so all eight workers stopping together is what we want.
Then, if they want more
The problem is that there's no recovery design around it. So I'd add four things.
In the worker, I'd catch the original exception, store the first one in a shared AtomicReference, and then break the barrier, either by cancelling the workers from a coordinator or with an interrupted await, so the others get BrokenBarrierException and exit quietly, treating it as a stop signal.
Second, I'd checkpoint in the barrier action, because it runs once per trip while everyone else is blocked, so the grid is consistent. With 30-second steps I might save every 50 steps, which costs well under one percent.
Third, a coordinator looks at the recorded error once all workers have stopped: a transient I/O or resource error gets a bounded retry from the last checkpoint, with reset or a fresh barrier; a deterministic numerical error aborts with the step number.
And if we wanted to continue with fewer workers or rebalance slices, I'd switch to a Phaser, since parties can register and deregister between phases.
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.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "A simulation runs 8 worker threads that each compute one slice of a grid per step and...": https://scrivio-interview-library.netlify.app/java/synchronizers/534d0e3816/