Fundamentals 13 min read

Java Concurrency Tools: CountDownLatch, CyclicBarrier, and Semaphore

This article explains three Java concurrency tools - CountDownLatch, CyclicBarrier, and Semaphore - detailing their mechanisms, source code analysis, and practical usage scenarios with code examples.

政采云技术
政采云技术
政采云技术
Java Concurrency Tools: CountDownLatch, CyclicBarrier, and Semaphore

This article provides a comprehensive overview of three essential Java concurrency tools: CountDownLatch, CyclicBarrier, and Semaphore. Each tool is explained with its core concepts, source code analysis, and practical usage examples.

CountDownLatch is introduced as a synchronization tool that allows one thread to wait for other threads to complete their tasks. The article explains its main methods including the constructor, await(), await(timeout, unit), and countDown(). The source code analysis reveals that CountDownLatch uses an internal Sync class that extends AbstractQueuedSynchronizer (AQS). The await() method calls AQS's acquireSharedInterruptibly(1), while countDown() calls releaseShared(1). A practical example simulates a 100-meter race where 10 runners start simultaneously and the main thread waits until all finish before declaring "Game Over."

CyclicBarrier is presented as a tool that allows a group of threads to wait for each other at a common barrier point. Unlike CountDownLatch, it's not based on AQS but uses a ReentrantLock and Condition. The article details its constructors and await() methods, explaining the core dowait() method that handles thread coordination. The source code shows how it uses a Generation class, a ReentrantLock, and a Condition to manage the barrier state. A usage example demonstrates three threads waiting at a barrier, with a barrier action executed when all threads arrive.

Semaphore is explained as a tool that controls access to shared resources using a counter. The article describes how it allows access when the counter is greater than zero and denies access when it's zero. Semaphore uses AQS internally and supports both fair and non-fair acquisition strategies. The source code analysis covers the acquire(), tryAcquire(), and release() methods, showing how they interact with the underlying AQS implementation. A practical example simulates four people using a restroom with only two available stalls, demonstrating how Semaphore limits concurrent access.

The article concludes by comparing CountDownLatch and CyclicBarrier, noting that CountDownLatch is typically used when one thread waits for others to complete, while CyclicBarrier is used when a group of threads wait for each other. It also highlights that CountDownLatch is not reusable, whereas CyclicBarrier is. The differences between fair and non-fair strategies in Semaphore are also discussed.

multithreadingSemaphoreAQSJava ConcurrencyThread SynchronizationCountDownLatchCyclicBarrier
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.