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