What is a Java CountDownLatch?

Synchronization is essential when you’re working with concurrency in the Java programming language. Java CountDownLatches are an effective tool to help you synchronize the operations of multiple threads. But what is a Java CountDownLatch exactly, and how do Java CountDownLatches work?

What are Java CountDownLatches?

When using parallel computing, it often makes sense to divide the work between multiple threads that each perform part of the computation. For example, if we want to compute the sum of all the numbers in an array, we could assign part of the array to each thread, which sums up only the numbers in the given subarray. We can then combine these individual sums to get our final answer.

But this approach has a major issue: if we perform the final sum with one of these threads, how can we be sure that the other threads have already finished their computations? Because multithreaded programs are generally non-deterministic, we cannot be sure in what order the threads will execute. Thus, it’s entirely possible that the final sum will be calculated before all the computations are complete, which will almost certainly give us an incorrect answer.

To fix this issue, we need to introduce synchronization: a concept in parallel computing that requires multiple threads to meet up at a given point or points during computation. This prevents one or more of the threads from racing ahead into critical sections, which are parts of the program that must only be executed by a single thread.

Java CountDownLatches are a type of synchronization primitive that are used to force threads to wait until the other threads have finished their given task. CountDownLatches are initialized with a count, i.e. an integer value that describes how many threads must synchronize with each other before computation can proceed. If a thread reaches this barrier before all other threads are finished, the CountDownLatch blocks the thread until all threads are waiting at the barrier.

How do Java CountDownLatches work?

Java CountDownLatches are implemented in the java.util.concurrent.CountDownLatch class. Here’s what you need to know about how Java CountDownLatches work:

  • When initializing a CountDownLatch in Java, we need to specify how many threads it should wait for, e.g. CountDownLatch latch = new CountDownLatch(10);
  • The await() method is used to force a thread to wait for any other threads that have yet to catch up when working with CountDownLatches in Java.
  • Threads need to call the CountDownLatch’s countDown() method to signal that they have finished their work. You can get the latch’s current count (i.e. the number of threads that have yet to complete) with the getCount() method.

CountDownLatches are sometimes contrasted with CyclicBarriers, another synchronization primitive in Java. Unlike the CyclicBarrier, the CountDownLatch is not able to reset the count once you start calling the countDown() method. In addition, there’s a bit of subtlety in the difference between CountDownLatches and CyclicBarriers:

  • With the CountDownLatch, some threads have completed their work and are waiting for others to finish.
  • With the CyclicBarrier, on the other hand, threads are waiting for each other before all of the threads arrive, at which point an action can optionally be taken.

Java CountDownLatches in Redis

Redis is an open-source in-memory data structure store used to implement NoSQL key-value databases, caches, and message brokers. Despite the utility and functionality of Redis, however, implementing concurrency and thread-safety in Redis is no easy feat. What’s more, developers used to programming languages such as Java, with the rich Java standard library, will struggle to adapt to Redis, which has only a few built-in data types.

To fix these issues and lower the learning curve of using Redis, many Java developers install a third-party Redis Java client such as Redisson. Redisson includes many familiar Java objects, collections, and constructs for developers using Redis and Java.

In Redisson, the CountDownLatch functionality is implemented using the RCountDownLatch interface. Below is a simple example demonstrating the use of the RCountDownLatch:

RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");

latch.trySetCount(1);
// await the countdown
latch.await();

// in other thread or JVM
RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
latch.countDown();

As the example shows, RCountDownLatch needs to be initialized with the trySetCount() method before using it. The await() method forces a thread to wait, while the countDown() method decrements the current value of the RCountDownLatch.

The RCountDownLatch also comes with Async, Reactive, and RxJava2 interfaces, so that you can choose the programming model that best fits your purposes. Below is a quick example reworking the above code to use the Async interface:

RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");

RFuture<Boolean> setFuture = lock.trySetCountAsync(1);
// await for count down
RFuture<Void> awaitFuture = latch.awaitAsync();

// in other thread or JVM
RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
RFuture<Void> countFuture = latch.countDownAsync();
Similar terms