CyclicBarrier
A CyclicBarrier waits until a fixed number of threads have called await, then releases them all and can be used again. An optional barrier action runs once per generation, by the last thread to arrive.
Detailed Explanation
A latch opens once and stays open. A barrier is a meeting point for a known party size. Each worker finishes its slice of a phase, calls await, and blocks. When the last worker arrives, the barrier trips, optionally runs a Runnable, and the same object is ready for the next phase. That is why it is cyclic.
new CyclicBarrier(n) or new CyclicBarrier(n, barrierAction). await returns the arrival index. await(timeout, unit) throws TimeoutException if the party is late; the barrier then breaks. A broken barrier throws BrokenBarrierException on later awaits until you reset. reset is delicate if threads are still waiting; prefer letting the generation fail and creating a new barrier if the protocol allows.
Use it for parallel phased algorithms: each thread computes a strip of a matrix, waits, then reads neighbours from the previous phase. The barrier action can swap buffers once, instead of every thread racing to swap.
You reach for a cyclic barrier when the same threads iterate phases together. You reach for a latch when one side only waits for "done" and will not meet again. You reach for Phaser when the party size changes. You reach for CompletableFuture when the work is a DAG of results, not a lock-step loop.
The cost of a missing await is a hang (or a timeout). The cost of a thread that dies before await is a broken barrier. Always await in finally if the thread should still arrive after a failed slice, or abort the whole job and reset with a documented protocol. Do not mix barrier waits with locks acquired in different orders; you can deadlock a phase.
Parties must be the threads that call await. A pool of size smaller than the party will deadlock: all workers sit in tasks that wait for more arrivals that have no thread left to run.
Code Examples
import java.util.concurrent.CyclicBarrier;
public class Phases {
public static void main(String[] args) throws Exception {
CyclicBarrier barrier = new CyclicBarrier(2, () -> System.out.println("phase"));
Thread t = new Thread(() -> {
try {
barrier.await();
} catch (Exception e) {
Thread.currentThread().interrupt();
}
});
t.start();
barrier.await();
t.join();
// prints: phase
}
}import java.util.concurrent.CyclicBarrier;
public class Size {
public static void main(String[] args) {
CyclicBarrier b = new CyclicBarrier(3);
System.out.println(b.getParties()); // prints: 3
System.out.println(b.getNumberWaiting()); // prints: 0
}
}Common Gotchas
Party size larger than the thread pool deadlocks
every worker is waiting and no thread is left to arrive.
A timeout or interrupt breaks the barrier
later `await`s throw until `reset`.
The barrier action runs on the last arriving thread
keep it short and safe.
`reset` while threads are in `await` is easy to get wrong
prefer a new instance after a failed generation.