Mastering CountDownLatch and CyclicBarrier: Real-World Java Concurrency Patterns
This article explains how CountDownLatch and CyclicBarrier work in Java, illustrates their usage with practical Dragon‑Ball analogies, compares their features, and provides code snippets and real‑time system scenarios to help developers synchronize threads effectively.
CountDownLatch
CountDownLatch is a practical multithreaded control tool introduced in Java 1.5 (java.util.concurrent). It allows one or more threads to wait until a set of other threads complete their tasks.
Example: seven threads (seven people) each find a Dragon Ball; only after all seven have returned does the main thread proceed, mimicking a countdown latch.
Running result shows that the main thread resumes only after countDownLatch.await() returns.
Key points:
The constructor new CountDownLatch(int count) sets the number of threads to wait for.
Each worker thread must call countDownLatch.countDown() when it finishes.
The main thread calls countDownLatch.await() and blocks until the count reaches zero.
CountDownLatch cannot be reset; it is a one‑time barrier.
Typical Use Cases of CountDownLatch in Real‑Time Systems
Achieving maximum parallelism: Initialize the latch with 1 and let all threads wait on it; a single countDown() releases them simultaneously.
Waiting for N threads before proceeding: Ensure that all external services are up before handling user requests.
Deadlock detection: Use varying numbers of threads to access shared resources and deliberately provoke deadlocks for testing.
CyclicBarrier
CyclicBarrier is another concurrency utility similar to CountDownLatch but reusable. It lets a group of threads wait at a barrier until the last thread arrives, then all are released.
Constructor: CyclicBarrier(int parties) where parties is the number of threads to block.
Each thread calls await() to signal it has reached the barrier.
Example (continuing the Dragon Ball story) uses two barrier points: first to gather the seven “mages”, then after they finish searching, they all proceed together.
Result shows two synchronization stages; the nested barrier demonstrates the concept of reusable barriers.
Calling CyclicBarrier.reset() returns the barrier to its initial state.
Differences Between CountDownLatch and CyclicBarrier
CountDownLatch is single‑use; CyclicBarrier can be reset and reused.
CyclicBarrier provides additional methods such as getNumberWaiting() and isBroken() for monitoring.
CountDownLatch typically blocks the main thread; CyclicBarrier blocks the participating worker threads.
Reference: http://www.importnew.com/15731.html
Note: Click the original article for full source code (PC view recommended).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
