Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Fundamentals 7 min read

CountDownLatch Explained: Java Thread Synchronization via Meeting Analogy

This article explains Java's CountDownLatch synchronization tool using a meeting analogy, demonstrates its usage with a Java 17 code example simulating attendees arriving at a meeting, and describes a real-world batch translation scenario where the main thread waits for multiple concurrent translation tasks to complete.

What is CountDownLatch?

Like programmers attending a requirements review meeting, where the meeting starts only when all participants—developers, testers, and product managers—have arrived, the meeting organizer (product manager) waits for everyone. This "wait until all arrive" scenario is common in programming.

For example, an e-commerce product detail page needs to fetch data from three services: product info, inventory, and coupons. To improve response time, three concurrent threads fetch data in parallel. However, the main thread must wait for all three subtasks to finish before assembling the response; if any data is missing, the page cannot render completely.

How does the main program know when all three threads have completed? This is where a meeting facilitator (main thread) and a sign-in clerk (synchronization tool CountDownLatch) come in. Note that the sign-in clerk is the CountDownLatch, not the main thread. CountDownLatch coordinates threads—here, the main thread and worker threads.

Mapping the Meeting Scenario to CountDownLatch

The following mapping illustrates the correspondence:

Sign-in clerk prepares a sheet listing 3 expected attendees → CountDownLatch latch = new CountDownLatch(3); Developer, tester, product manager arrive and report to clerk → Each worker thread calls latch.countDown() upon completion (counter decrements by 1)

Facilitator asks clerk: "Is everyone here?" Clerk replies: "Not yet, wait." → Main thread calls latch.await() and blocks

Last person reports; clerk sees zero remaining, all present! → Counter reaches zero

Clerk wakes facilitator: "Meeting can start." → CountDownLatch unblocks main thread; await() returns and main thread continues

Code Simulation with Java 17

The following Java 17 code simulates the scenario: the main thread acts as facilitator, a CountDownLatch as sign-in clerk, and three threads represent the attendees.

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;

public class MeetingDemo {
    public static void main(String[] args) throws Exception {
        // Sign-in sheet: 3 expected attendees (developer, tester, product manager)
        CountDownLatch latch = new CountDownLatch(3);
        String[] roles = {"Developer", "Tester", "Product Manager"};

        // Each attendee runs in a separate thread, walking to the meeting room
        for (String role : roles) {
            new Thread(() -> {
                try {
                    // Walking time varies between 300 and 800 milliseconds
                    Thread.sleep(ThreadLocalRandom.current().nextInt(300, 800));
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                System.out.println(role + " arrived at meeting room");
                // Attendee reports to clerk; clerk crosses off one name
                latch.countDown();
            }).start();
        }

        // Facilitator (main thread) waits at clerk; blocks until all names crossed off
        latch.await();
        System.out.println("All arrived, meeting starts");
    }
}

Sample output (order varies due to random sleep times):

Product manager arrived at meeting room
Developer arrived at meeting room
Tester arrived at meeting room
All arrived, meeting starts

Each attendee thread sleeps a random duration (300–800 ms) to simulate different arrival times. Upon arrival, each calls countDown() to decrement the counter. The main thread calls await() and blocks until the counter reaches zero, then prints the start message.

Real-World Business Scenario: Batch Translation

In a production environment for a global chain-store business (stores in Japan, USA, Macau, Hong Kong, etc.), a "one-click translation" feature sends batches of copy to an external translation service for multiple languages. Each call takes several hundred milliseconds. To reduce latency, translations run concurrently, and the main thread waits for all to complete before assembling results for the frontend.

Any situation where the main thread must wait for multiple worker threads to finish before proceeding is a candidate for CountDownLatch.

This article aims to introduce CountDownLatch; its internal implementation will be covered in a future post.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

MultithreadingJava concurrencyThread SynchronizationCountDownLatchJava 17Batch TranslationConcurrency Patterns

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion