Files
asmhatreandClaude Sonnet 5 4a74153d87 hashmap-concurrenthashmap: companion code for the HashMap vs ConcurrentHashMap interview guide
Adds JMH put/get throughput benchmarks (synchronizedMap vs ConcurrentHashMap,
swept at 1/2/4/8 threads), a computeIfAbsent recursion trap demo (CHM tolerates
recursion into a different key but throws IllegalStateException on the same
key; plain HashMap throws ConcurrentModificationException even single-threaded),
and a null-handling demo (CHM rejects null keys/values on put() and null keys
on get() too). 7/7 JUnit tests pass; all output/ transcripts captured from
real runs.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FhzLY5p6okFva3qsnsRyvM
2026-09-30 09:29:06 +00:00

92 lines
6.2 KiB
Plaintext

$ java -jar benchmarks.jar PutGetBenchmark -t 4
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/home/claude/java-core-examples/hashmap-concurrenthashmap/target/benchmarks.jar)
WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
# JMH version: 1.37
# VM version: JDK 25.0.4.1, OpenJDK 64-Bit Server VM, 25.0.4.1+1-LTS
# VM invoker: /tmp/tools/jdk-25.0.4.1+1/bin/java
# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=36265 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes=
# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable)
# Warmup: 3 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.ankurm.hashmapconcurrenthashmap.PutGetBenchmark.concurrentHashMapPutGet
# Run progress: 0.00% complete, ETA 00:00:16
# Fork: 1 of 1
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/home/claude/java-core-examples/hashmap-concurrenthashmap/target/benchmarks.jar)
WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
# Warmup Iteration 1: 40669.632 ops/ms
# Warmup Iteration 2: 42866.944 ops/ms
# Warmup Iteration 3: 46923.803 ops/ms
Iteration 1: 37839.870 ops/ms
Iteration 2: 47224.405 ops/ms
Iteration 3: 48408.338 ops/ms
Iteration 4: 47520.187 ops/ms
Iteration 5: 50097.930 ops/ms
Result "com.ankurm.hashmapconcurrenthashmap.PutGetBenchmark.concurrentHashMapPutGet":
46218.146 ±(99.9%) 18542.465 ops/ms [Average]
(min, avg, max) = (37839.870, 46218.146, 50097.930), stdev = 4815.419
CI (99.9%): [27675.681, 64760.611] (assumes normal distribution)
# JMH version: 1.37
# VM version: JDK 25.0.4.1, OpenJDK 64-Bit Server VM, 25.0.4.1+1-LTS
# VM invoker: /tmp/tools/jdk-25.0.4.1+1/bin/java
# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=36265 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes=
# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable)
# Warmup: 3 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.ankurm.hashmapconcurrenthashmap.PutGetBenchmark.synchronizedHashMapPutGet
# Run progress: 50.00% complete, ETA 00:00:08
# Fork: 1 of 1
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/home/claude/java-core-examples/hashmap-concurrenthashmap/target/benchmarks.jar)
WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
# Warmup Iteration 1: 7626.664 ops/ms
# Warmup Iteration 2: 8530.844 ops/ms
# Warmup Iteration 3: 9799.982 ops/ms
Iteration 1: 9978.021 ops/ms
Iteration 2: 9500.059 ops/ms
Iteration 3: 8813.332 ops/ms
Iteration 4: 9502.575 ops/ms
Iteration 5: 9466.454 ops/ms
Result "com.ankurm.hashmapconcurrenthashmap.PutGetBenchmark.synchronizedHashMapPutGet":
9452.088 ±(99.9%) 1598.912 ops/ms [Average]
(min, avg, max) = (8813.332, 9452.088, 9978.021), stdev = 415.232
CI (99.9%): [7853.176, 11051.000] (assumes normal distribution)
# Run complete. Total time: 00:00:17
REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.
NOTE: Current JVM experimentally supports Compiler Blackholes, and they are in use. Please exercise
extra caution when trusting the results, look into the generated code to check the benchmark still
works, and factor in a small probability of new VM bugs. Additionally, while comparisons between
different JVMs are already problematic, the performance difference caused by different Blackhole
modes can be very significant. Please make sure you use the consistent Blackhole mode for comparisons.
Benchmark Mode Cnt Score Error Units
PutGetBenchmark.concurrentHashMapPutGet thrpt 5 46218.146 ± 18542.465 ops/ms
PutGetBenchmark.synchronizedHashMapPutGet thrpt 5 9452.088 ± 1598.912 ops/ms