locks: synchronized vs ReentrantLock vs StampedLock companion code
JMH throughput sweep (1-64 threads), 9:1 read-heavy @Group benchmark, tryLock(timeout) deadlock-avoidance demo, and a JDK21-vs-JDK25 JEP 491 virtual-thread-pinning comparison. Also promotes LICENSE to repo root now that a second module exists.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
$ java -version
|
||||
openjdk version "21.0.10" 2026-01-20
|
||||
OpenJDK Runtime Environment (build 21.0.10+7-Ubuntu-124.04)
|
||||
OpenJDK 64-Bit Server VM (build 21.0.10+7-Ubuntu-124.04, mixed mode, sharing)
|
||||
|
||||
$ java -Djdk.tracePinnedThreads=full com.ankurm.locks.VirtualThreadPinningDemo
|
||||
VirtualThread[#18]/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.locks.VirtualThreadPinningDemo.lambda$main$0(VirtualThreadPinningDemo.java:25) <== monitors:1
|
||||
java.base/java.lang.VirtualThread.run(VirtualThread.java:329)
|
||||
Virtual thread finished. (No output above this line means it did not pin.)
|
||||
@@ -0,0 +1,7 @@
|
||||
$ java -version
|
||||
openjdk version "25.0.4.1" 2026-08-18 LTS
|
||||
OpenJDK Runtime Environment Temurin-25.0.4.1+1 (build 25.0.4.1+1-LTS)
|
||||
OpenJDK 64-Bit Server VM Temurin-25.0.4.1+1 (build 25.0.4.1+1-LTS, mixed mode, sharing)
|
||||
|
||||
$ java -Djdk.tracePinnedThreads=full com.ankurm.locks.VirtualThreadPinningDemo
|
||||
Virtual thread finished. (No output above this line means it did not pin.)
|
||||
@@ -0,0 +1,9 @@
|
||||
$ java -cp target/classes com.ankurm.locks.TryLockTimeoutDemo
|
||||
Thread-2: timed out waiting for second lock on attempt 1 - backing off and retrying.
|
||||
Thread-1: timed out waiting for second lock on attempt 1 - backing off and retrying.
|
||||
Thread-2: timed out waiting for second lock on attempt 2 - backing off and retrying.
|
||||
Thread-1: timed out waiting for second lock on attempt 2 - backing off and retrying.
|
||||
Thread-2: timed out waiting for second lock on attempt 3 - backing off and retrying.
|
||||
Thread-1: acquired both locks on attempt 3.
|
||||
Thread-2: acquired both locks on attempt 4.
|
||||
Both threads finished in 808 ms - no deadlock.
|
||||
@@ -0,0 +1,52 @@
|
||||
$ java -jar target/benchmarks.jar IncrementBenchmark -t <N> -rf text
|
||||
(one JVM invocation per thread count; -t sets the JMH thread count for that run)
|
||||
|
||||
=== threads=1 ===
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
IncrementBenchmark.reentrantLockFair thrpt 5 52315.361 ± 7000.813 ops/ms
|
||||
IncrementBenchmark.reentrantLockUnfair thrpt 5 52462.260 ± 3295.127 ops/ms
|
||||
IncrementBenchmark.stampedLockWrite thrpt 5 51466.587 ± 12682.048 ops/ms
|
||||
IncrementBenchmark.synchronized_ thrpt 5 43053.756 ± 4276.407 ops/ms
|
||||
|
||||
=== threads=2 ===
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
IncrementBenchmark.reentrantLockFair thrpt 5 984.984 ± 1624.886 ops/ms
|
||||
IncrementBenchmark.reentrantLockUnfair thrpt 5 13316.608 ± 4428.282 ops/ms
|
||||
IncrementBenchmark.stampedLockWrite thrpt 5 15243.885 ± 3805.083 ops/ms
|
||||
IncrementBenchmark.synchronized_ thrpt 5 10285.533 ± 6953.617 ops/ms
|
||||
|
||||
=== threads=4 ===
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
IncrementBenchmark.reentrantLockFair thrpt 5 149.465 ± 598.480 ops/ms
|
||||
IncrementBenchmark.reentrantLockUnfair thrpt 5 43978.589 ± 8289.565 ops/ms
|
||||
IncrementBenchmark.stampedLockWrite thrpt 5 50067.586 ± 4023.378 ops/ms
|
||||
IncrementBenchmark.synchronized_ thrpt 5 7373.456 ± 5254.126 ops/ms
|
||||
|
||||
=== threads=8 ===
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
IncrementBenchmark.reentrantLockFair thrpt 5 77.622 ± 9.397 ops/ms
|
||||
IncrementBenchmark.reentrantLockUnfair thrpt 5 50180.739 ± 8707.699 ops/ms
|
||||
IncrementBenchmark.stampedLockWrite thrpt 5 54031.902 ± 4571.353 ops/ms
|
||||
IncrementBenchmark.synchronized_ thrpt 5 7431.158 ± 2055.967 ops/ms
|
||||
|
||||
=== threads=16 ===
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
IncrementBenchmark.reentrantLockFair thrpt 5 81.785 ± 23.399 ops/ms
|
||||
IncrementBenchmark.reentrantLockUnfair thrpt 5 45460.231 ± 5190.390 ops/ms
|
||||
IncrementBenchmark.stampedLockWrite thrpt 5 46998.078 ± 3362.893 ops/ms
|
||||
IncrementBenchmark.synchronized_ thrpt 5 8185.253 ± 3341.605 ops/ms
|
||||
|
||||
=== threads=32 ===
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
IncrementBenchmark.reentrantLockFair thrpt 5 71.593 ± 24.857 ops/ms
|
||||
IncrementBenchmark.reentrantLockUnfair thrpt 5 45325.559 ± 1160.775 ops/ms
|
||||
IncrementBenchmark.stampedLockWrite thrpt 5 46630.727 ± 4303.326 ops/ms
|
||||
IncrementBenchmark.synchronized_ thrpt 5 9378.353 ± 7605.473 ops/ms
|
||||
|
||||
=== threads=64 ===
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
IncrementBenchmark.reentrantLockFair thrpt 5 69.470 ± 5.015 ops/ms
|
||||
IncrementBenchmark.reentrantLockUnfair thrpt 5 44724.521 ± 5166.580 ops/ms
|
||||
IncrementBenchmark.stampedLockWrite thrpt 5 46973.404 ± 3639.073 ops/ms
|
||||
IncrementBenchmark.synchronized_ thrpt 5 12510.129 ± 3496.906 ops/ms
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
$ java -jar target/benchmarks.jar ReadHeavyBenchmark -rf text
|
||||
(9 reader threads : 1 writer thread per lock type, via JMH @Group/@GroupThreads)
|
||||
|
||||
Benchmark Mode Cnt Score Error Units
|
||||
ReadHeavyBenchmark.reentrantLock thrpt 5 45225.344 ± 3890.955 ops/ms
|
||||
ReadHeavyBenchmark.reentrantLock:reentrantLockRead thrpt 5 39936.906 ± 3728.160 ops/ms
|
||||
ReadHeavyBenchmark.reentrantLock:reentrantLockWrite thrpt 5 5288.438 ± 1952.067 ops/ms
|
||||
ReadHeavyBenchmark.stampedLock thrpt 5 915584.241 ± 68486.471 ops/ms
|
||||
ReadHeavyBenchmark.stampedLock:stampedLockRead thrpt 5 914889.465 ± 69210.992 ops/ms
|
||||
ReadHeavyBenchmark.stampedLock:stampedLockWrite thrpt 5 694.777 ± 1802.294 ops/ms
|
||||
ReadHeavyBenchmark.synchronizedCounter thrpt 5 16320.540 ± 4834.389 ops/ms
|
||||
ReadHeavyBenchmark.synchronizedCounter:synchronizedRead thrpt 5 15319.823 ± 5526.566 ops/ms
|
||||
ReadHeavyBenchmark.synchronizedCounter:synchronizedWrite thrpt 5 1000.717 ± 860.855 ops/ms
|
||||
@@ -0,0 +1,4 @@
|
||||
-------------------------------------------------------------------------------
|
||||
Test set: com.ankurm.locks.CounterCorrectnessTest
|
||||
-------------------------------------------------------------------------------
|
||||
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.218 s -- in com.ankurm.locks.CounterCorrectnessTest
|
||||
Reference in New Issue
Block a user