CyclicBarrier: concordinating the stages of a multithreaded operation
The CountDownLatch class is useful for various types of "one-off" thread
coordination, in particular setting threads off together. However, it has
at least two features that can be inconvenient in certain situations:
- a given CountDownLatch can only be used once, making it inconvenient for
operations that occur in stages, with intermediate results from the different threads
needing to be amalgamated between stages;
- the CountDownLatch doesn't explicitly allow one thread
to tell the others to "stop waiting"1, which is sometimes useful, for example,
if an error occurs in one of the threads.
The CyclicBarrier is generally more useful than CountDownLatch in
- a multithreaded operation occurs in stages or iterations,
- a single-threaded operation is required between stages/iterations, for example,
to combine the results of the previous multithreaded portion.
Overview of CyclicBarrier
Firstly, the barrier is constructed with the following:
- the number of threads that will be participating in the parallel operation;
- optionally, an amalgamation routine to run at the end of each stage/iteration.
Then, at each stage (or on each iteration) of the operation:
- each thread carries out its portion of the work;
- after doing its portion of the work, each thread calls the barrier's await()
- the await() method returns only when:
- all threads have called await();
- the amalgamation method has run (the barrier calls this on the last thread
to call await() before releasing the awaiting threads).
- if any of the threads is interrupted or times out while waiting for
the barrier, then the barrier is "broken" and all other waiting threads
receive a BrokenBarrierException.
in other words, this last point means there is a mechanism for
an error/timeout in one of the worker threads to "ripple out" to all threads and for the
operation to halt, or for the operation to be interrupted externally by interrupting
just one of the threads.
Example: parallel sort
On the next page, we look at a
parallel sort using CyclicBarrier, as an
example of how to use the class.
1. Provided you know which threads are awaiting a latch,
you can of course interrupt them.