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:
2026-09-30 06:04:50 +00:00
parent f548d9eeb5
commit 32b8067ecf
25 changed files with 763 additions and 6 deletions
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# Regenerates every file in output/ using the same commands used to produce the ones
# committed here. Requires JDK 25 on PATH/JAVA_HOME for the main build, plus a JDK 21
# install for output/01 (see the comment above that command below).
set -euo pipefail
cd "$(dirname "$0")/.."
mvn -q package
echo "--- 02: pinning demo (JDK 25, post-JEP-491) ---"
java -cp target/classes -Djdk.tracePinnedThreads=full com.ankurm.locks.VirtualThreadPinningDemo \
> output/02-pinning-jdk25-post-jep491.txt 2>&1 || true
echo "--- 03: tryLock timeout deadlock avoidance ---"
java -cp target/classes com.ankurm.locks.TryLockTimeoutDemo \
> output/03-trylock-timeout-deadlock-avoidance.txt 2>&1
echo "--- 04: IncrementBenchmark thread-count sweep (takes a few minutes) ---"
{
echo '$ java -jar target/benchmarks.jar IncrementBenchmark -t <N> -rf text'
for t in 1 2 4 8 16 32 64; do
echo "=== threads=$t ==="
java -jar target/benchmarks.jar "IncrementBenchmark" -t "$t" -rf text -rff /tmp/jmh-increment-$t.txt
cat "/tmp/jmh-increment-$t.txt"
echo ""
done
} > output/04-increment-benchmark-sweep.txt
echo "--- 05: ReadHeavyBenchmark (9:1 read:write via @Group) ---"
java -jar target/benchmarks.jar "ReadHeavyBenchmark" -rf text -rff output/05-read-heavy-benchmark.txt
echo "--- 06: correctness tests ---"
mvn -q test
cp target/surefire-reports/com.ankurm.locks.CounterCorrectnessTest.txt output/06-counter-correctness.txt
echo "--- 01: pinning demo on JDK 21 (pre-JEP-491) - needs a separate JDK 21 install ---"
echo " javac --release 21 -d /tmp/pinning-jdk21-classes src/main/java/com/ankurm/locks/VirtualThreadPinningDemo.java"
echo " java -cp /tmp/pinning-jdk21-classes -Djdk.tracePinnedThreads=full com.ankurm.locks.VirtualThreadPinningDemo"
+14
View File
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Quick ad hoc JMH run against one benchmark class, printed to the terminal. Example:
# ./scripts/run.sh IncrementBenchmark -t 8
# ./scripts/run.sh ReadHeavyBenchmark
set -euo pipefail
cd "$(dirname "$0")/.."
BENCH="${1:?Usage: run.sh <BenchmarkClassName> [extra jmh args]}"
shift || true
JAR=target/benchmarks.jar
[ -f "$JAR" ] || { echo "Run 'mvn package' first."; exit 1; }
java -jar "$JAR" "$BENCH" "$@"