Java Benchmark Reveals CAS vs. Lock Tradeoffs and a Critical JMH Annotation Pitfall
A Java developer benchmarked a hand-rolled compare-and-swap (CAS) retry loop against a synchronized lock-based counter using JMH across 1 to 8 threads on 4-core hardware. The experiment showed CAS is approximately 2.1x faster than a synchronized lock at a single thread, but throughput collapsed sharply as thread count increased due to contention. A missing JMH @OperationsPerInvocation annotation on just one of the two implementations was found to distort results by a factor of up to 476,000x, effectively flipping the conclusion about which approach was faster. Even a symmetrical omission of the annotation on both sides would preserve the relative ratio but render the absolute throughput figures meaningless. The findings highlight how subtle benchmark configuration errors can produce confidently wrong results rather than just noisy ones.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in