Concurrency

Synchronizers: Latches, Barriers, Semaphores

Coordination primitives for thread rendezvous: CountDownLatch (wait for N events, once), CyclicBarrier (N parties meet repeatedly), Semaphore (at most N concurrent holders), Phaser (all of the above, dynamic).
  • CountDownLatch(n): await() blocks until countDown() reaches zero; single-use
  • CyclicBarrier(n, action): parties await() each other; reusable per generation
  • Semaphore(permits): acquire/release bound concurrent access to a resource
  • Phaser: register/deregister parties dynamically across phases
  • Prefer these over wait/notify — always (EJ 81)
Latch: start gate + completion gate (JCiP 5.5.1)
CountDownLatch start = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(workers);

for (int i = 0; i < workers; i++) {
    pool.submit(() -> {
        start.await();            // everyone waits for the gun
        try { work(); }
        finally { done.countDown(); }
        return null;
    });
}
long t0 = System.nanoTime();
start.countDown();                // fire!
done.await();                     // wait for all to finish
long elapsed = System.nanoTime() - t0;
Semaphore: bounding concurrency
private final Semaphore dbSlots = new Semaphore(10);   // at most 10 concurrent queries

public Result query(String sql) throws InterruptedException {
    dbSlots.acquire();
    try {
        return db.run(sql);
    } finally {
        dbSlots.release();
    }
}

Semaphores are the general resource-bounding tool: connection limits, rate limiting, turning any collection into a bounded one (JCiP 5.5.3). With Virtual Threads they replace pool-sizing as the way to limit concurrent access to a constrained resource — a million virtual threads, ten permits.

CyclicBarrier suits iterative simulations: N workers compute a step, await() (the barrier action merges results), then everyone proceeds to the next generation. Exchanger<V> swaps objects between exactly two threads — the classic buffer-swap between a filler and a drainer.

Sources
  • Java Concurrency in PracticeCh. 5.5, 14 — Synchronizers; Building Custom Synchronizers
  • Effective Java (3rd ed.)Item 81
  • Core Java, Volume I: Fundamentals (13th ed.)Ch. 10.4 — Coordinating Tasks