synchronizers: CountDownLatch vs CyclicBarrier vs Phaser vs Semaphore

One three-round worker pipeline implemented with CountDownLatch (one latch
per round), CyclicBarrier (reusable, with a barrier action), and Phaser
(reusable, plus dynamic mid-run registration), alongside Semaphore solving
the genuinely different problem of bounding concurrent access. Covers
timeout behavior (CyclicBarrier permanently breaks on one timeout, Phaser
doesn't) and virtual-thread compatibility, including the corrected finding
that Object.wait() releases its monitor and never pinned, unlike
Thread.sleep() inside synchronized.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FhzLY5p6okFva3qsnsRyvM
This commit is contained in:
2026-09-30 06:43:52 +00:00
co-authored by Claude Sonnet 5
parent 0819993c51
commit 55ee850a75
20 changed files with 962 additions and 0 deletions
@@ -0,0 +1,17 @@
$ java CountDownLatchPipeline
main: all 4 workers created, still waiting at the start gate
main: releasing the start gate
worker-0: finished round 1
worker-1: finished round 1
worker-2: finished round 1
worker-3: finished round 1
worker-0: finished round 2
worker-1: finished round 2
worker-2: finished round 2
worker-3: finished round 2
worker-0: finished round 3
worker-1: finished round 3
worker-2: finished round 3
worker-3: finished round 3
done
@@ -0,0 +1,18 @@
$ java CyclicBarrierPipeline
worker-0: finished round 1
worker-1: finished round 1
worker-2: finished round 1
worker-3: finished round 1
barrier action: round 1 complete, all 4 workers arrived
worker-0: finished round 2
worker-1: finished round 2
worker-2: finished round 2
worker-3: finished round 2
barrier action: round 2 complete, all 4 workers arrived
worker-0: finished round 3
worker-1: finished round 3
worker-2: finished round 3
worker-3: finished round 3
barrier action: round 3 complete, all 4 workers arrived
done
@@ -0,0 +1,21 @@
$ java PhaserPipeline
worker-0: finished round 1
worker-1: finished round 1
worker-2: finished round 1
worker-3: finished round 1
phaser onAdvance: phase 1 complete, 4 registered parties -> advancing
worker-4: registers after round 1 (phaser now has 5 parties)
worker-0: finished round 2
worker-1: finished round 2
worker-2: finished round 2
worker-3: finished round 2
worker-4: finished round 2
phaser onAdvance: phase 2 complete, 5 registered parties -> advancing
worker-0: finished round 3
worker-1: finished round 3
worker-2: finished round 3
worker-3: finished round 3
worker-4: finished round 3
phaser onAdvance: phase 3 complete, 5 registered parties -> advancing
done
@@ -0,0 +1,16 @@
$ java SemaphoreResourceGuard
worker-0 round 1: acquired permit, 1 callers concurrently in the resource (availablePermits=0)
worker-1 round 1: acquired permit, 2 callers concurrently in the resource (availablePermits=0)
worker-2 round 1: acquired permit, 2 callers concurrently in the resource (availablePermits=0)
worker-3 round 1: acquired permit, 2 callers concurrently in the resource (availablePermits=0)
worker-3 round 2: acquired permit, 1 callers concurrently in the resource (availablePermits=1)
worker-0 round 2: acquired permit, 2 callers concurrently in the resource (availablePermits=0)
worker-1 round 2: acquired permit, 1 callers concurrently in the resource (availablePermits=1)
worker-2 round 2: acquired permit, 2 callers concurrently in the resource (availablePermits=0)
worker-2 round 3: acquired permit, 1 callers concurrently in the resource (availablePermits=1)
worker-3 round 3: acquired permit, 2 callers concurrently in the resource (availablePermits=0)
worker-0 round 3: acquired permit, 1 callers concurrently in the resource (availablePermits=1)
worker-1 round 3: acquired permit, 2 callers concurrently in the resource (availablePermits=0)
permits=2 maxObservedConcurrency=2
done
@@ -0,0 +1,20 @@
$ java TimeoutBehaviorDemo
=== CountDownLatch.await(timeout): expects 2, only 1 ever counts down ===
await returned: false (false = timed out, latch still usable to re-check later)
getCount() after timeout: 1
=== CyclicBarrier.await(timeout): expects 2 parties, only 1 arrives ===
await threw TimeoutException, as expected
barrier.isBroken() after the timeout: true
a second, later await() on the SAME barrier: BrokenBarrierException immediately - one timeout poisons the barrier for everyone until reset()
=== Phaser.awaitAdvanceInterruptibly(phase, timeout): expects 2 parties, only 1 arrives ===
awaitAdvanceInterruptibly threw TimeoutException, as expected
phaser.getPhase() after the timeout: 0 (unchanged - still phase 0, not advanced or broken)
a later round on the SAME phaser, once both parties actually arrive: succeeds normally, now at phase 1
=== Semaphore.tryAcquire(timeout): 0 permits available ===
tryAcquire returned: false (false = timed out, no exception, no broken state)
a later tryAcquire after a release: true - fully independent of the earlier timeout
done
@@ -0,0 +1,25 @@
$ java -Djdk.tracePinnedThreads=full VirtualThreadCompatibilityDemo (pre-JDK-24 runtime)
java.version=21.0.10
jdk.tracePinnedThreads=full
CountDownLatch.await(): completed, no trace above this line for it
CyclicBarrier.await(): completed, no trace above this line for it
Phaser.arriveAndAwaitAdvance(): completed, no trace above this line for it
Semaphore.acquire(): completed, no trace above this line for it
done - no pinning trace printed above for any of the four
$ java -Djdk.tracePinnedThreads=full SynchronizedWaitPinsDemo (pre-JDK-24 runtime, contrast case)
java.version=21.0.10
jdk.tracePinnedThreads=full
synchronized + Object.wait(): completed, no pinning trace above this line for it
VirtualThread[#21]/runnable@ForkJoinPool-1-worker-1 reason:MONITOR
java.base/java.lang.VirtualThread$VThreadContinuation.onPinned(VirtualThread.java:199)
java.base/jdk.internal.vm.Continuation.onPinned0(Continuation.java:393)
java.base/java.lang.VirtualThread.parkNanos(VirtualThread.java:635)
java.base/java.lang.VirtualThread.sleepNanos(VirtualThread.java:807)
java.base/java.lang.Thread.sleep(Thread.java:507)
com.ankurm.synchronizers.SynchronizedWaitPinsDemo.lambda$main$1(SynchronizedWaitPinsDemo.java:48) <== monitors:1
java.base/java.lang.VirtualThread.run(VirtualThread.java:329)
synchronized + Thread.sleep(): completed, pinning trace (if any) appears above this line for it
done
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.ankurm.synchronizers.RoundOrderingCorrectnessTest
-------------------------------------------------------------------------------
Tests run: 41, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.514 s -- in com.ankurm.synchronizers.RoundOrderingCorrectnessTest