Java threading introduction
Threading with Swing
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.
Java threading articles
Java threading and concurrency
Java performance graph index
Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey.
Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles,
or if you simply have a programming question that you would like to see answered on this site. Most topics
will be considered. But in particular, the site aims to provide tutorials and information on topics that
aren't well covered elsewhere, or on Java performance information that is poorly described or understood.
Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Javamex UK 2016. All rights reserved.