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:
@@ -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
|
||||
Reference in New Issue
Block a user