« Java 7 Roundup (November 3rd) | Home | Java 7 Roundup (November 11th) »
Thread coordination with CountDownLatch and CyclicBarrier
Java 5 introduced many new concurrency primitives and collections, and this post is going to look at two classes that can be used to coordinate threads: CountDownLatch and CyclicBarrier.
A CountDownLatch is initialized with a counter. Threads can then either count down on the latch or wait for it to reach 0. When the latch reaches 0, all waiting threads are released.
A common idiom is to use a latch to trigger a coordinated start or end between threads:
In this code, you’ll see two latches get initialized. Each thread that starts up counts down on the latch and awaits the latch counting down to 0 (when all threads have been initialized). Similarly, each thread waits for all threads to complete at the same time.
Running this program yields:
You can see that each thread hits run() at different times, but proceeds past the barrier at the same time. They each then do some random amount of work and wait for the latch, then proceed past it together.
In the example above, each thread waits forever for the latch to trigger. You can also choose to wait for a specified time period before giving up. And you can check the latch to see how many threads have arrived and are now waiting. Each CountDownLatch instance can only be used once and is then dead.
If you want a set of threads to repeatedly meet at a common point, you are better served by using a CyclicBarrier. A common use for this is in multi-threaded testing where it is typical to start a bunch of threads, meet, do some stuff, meet, validate some assertions, repeatedly.
The prior program can be simplified by replacing the two latches with a single barrier:
We can see here that the threads can repeatedly wait at the barrier, which implicitly counts down until all threads have arrived, then releases all threads.
Another nice trick with CyclicBarrier is that a Runnable action can be associated with the barrier to be run by the last thread reaching the barrier. You can very simply build a start/end timer for testing with this functionality:
Here we rely on knowing that the barrier will be reached exactly twice - once at start and once at end. The first time it’s reached, we record a timestamp and the second time it’s reached we print out the timing. When we construct our barrier, we give it an instance of this timer class. Each thread then waits to start on the barrier, works for a random amount of time, and waits for the end barrier.
A run looks like this:
Generally, you should expect the recorded elapsed time to be the maximum of the working time of any of the threads.
CyclicBarrier has a few additional tricks as well - threads can wait for a time period instead of forever, check whether a barrier has been broken (by interruption or forcibly with a reset() method), and determine the number of parties and the number currently waiting.
This is the first of a series of occasional posts on the concurrency classes added in Java 5 on. Next in the series will be a little program I call “The Hungry Philosophers”.
About this entry
You’re currently reading “Thread coordination with CountDownLatch and CyclicBarrier,” an entry on Pure Danger Tech
- Published:
- Nov 11 2007 / 2:50 pm
- Category:
- Java, jdk5, concurrency
- Vote:
- Other posts with these tags:

1 Comment
Jump to comment form | comments rss | trackback uri