Java concurrency utilities have kept evolving and provides many different ways to achieve similar tasks. Recently, we had a task to implement a concurrent counter. This triggered my interest in comparing different ways and their performance under various read and write workload.
The end result is a simple concurrent counter implemented in various ways:
- synchronized
- volatile
- AtomicLong
- LongAdder
- ReentrantLock in regular and fair mode
- ReentrantReadWriteLock in regular and fair mode
- StampedLock in regular read/write and optimistic read lock
- Semaphore in regular and fair mode
The benchmark is implemented using JMH, the standard way for reliable Java performance microbenchmark. You can find several really nice tutorials on JMH in the References section.
In my benchmark, there are write and read operations on the counter. The write takes 10ms and read takes 2ms. I set the number of read and write threads to simulate different mix of the workload scenarios using JMH group.
Both the source code and benchmark raw data, Excel sheets and visualizations can be found in the git repo: java-concurrency-counters-benchmark.
Here is a quick summary based on my experiment (I only set 2 rounds of warmups and 2 rounds of benchmark due to limited time):
- AtomicLong and LongAdder has similar throughput. In read-heavy workloads, AtomicLong has better read and write throughput than LongAdder. In write-heavy workloads, LongAdder has slightly better write throughput.
- Fair lock has lower throughput than regular lock in general, but not always.
- Consider using ReentrantLock or ReentrantReadWriteLock if you need high read throughput and the concurrency level is high.
- StampedLock provides very good write throughput in all the read-write mixes, if write throughput is important to you, you can try it. At the same time, if you need comparatively good read throughput, try optimistic read StampedLock. It has really good read throughput when concurrency level is high compared with regular StampedLock.
Special thanks and references:
- Isuru's blog post: Benchmarking Java Locks with Counters and the git repo. I found out this in the middle of my experiment and found it was doing almost the same tests. Based on his blog, I made further improvements on mine. Comparing to his great work, I tried several more combinations, pushed read and write thread count higher and reported read and write results separately.
- Takipi's blog post on Java 8 LongAdder performance and Java 8 StampedLock performance. And their git repo for counter benchmark (they did not use jmh somehow).
- Jakob Jenkov's great beginner's intro to JMH, all his series are very enjoyable and informative readings.
- java-performance.info posts: Introduction to JMH and Introduction to JMH Profilers.
- Nitsan Wakart's posts: JMH Resources, Writing Java Micro Benchmarks with JMH: Juice, Using JMH to Benchmark Multi-Threaded Code
Java Concurrency Counters are very nice and reliable. I am the user of this product since long. It helps me in counting the different objects when I need to count and remember the total.
ReplyDeleteFrom your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
ReplyDeletejava training in chennai | java training in bangalore
java online training | java training in pune
selenium training in chennai
selenium training in bangalore
This comment has been removed by the author.
ReplyDeleteAwesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeleteangularjs Training in online
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in btm
A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
ReplyDeletepython training in chennai
python training in Bangalore
Python training institute in chennai
I really like your blog. You make it interesting to read and entertaining at the same time. I cant wait to read more from you.
ReplyDeleteangularjs Training in marathahalli
angularjs interview questions and answers
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in chennai
automation anywhere online Training
Thanks for share information....KEep up sharing. drama wiki
ReplyDeleteGreat blog. You put Good stuff. All the topics were explained briefly.so quickly understand for media am waiting for your next fantastic blog. Thanks for sharing.
ReplyDeleteDevops Training in Chennai | Devops Training Institute in Chennai
wonderful thanks for sharing an amazing idea. keep it...
ReplyDeleteeTechno Soft Solutions offers the industry recognized Job Oriented Training in Bangalore that combines corporate training, online training, and classroom training effectively to fulfill the educational demands of the students worldwide.