What is StampedLock in Java?

前端 未结 4 1669
清酒与你
清酒与你 2020-12-24 12:50

I am working on a Java code, I need to implement threading in it. I was going through JAVA 8 API and I come to know about Stamped Locks. Can anyone tell me why to use Stampe

4条回答
  •  一整个雨季
    2020-12-24 13:29

    StampedLock is an alternative to using a ReadWriteLock (implemented by ReentrantReadWriteLock). The main differences between StampedLock and ReentrantReadWriteLock are that:

    • StampedLocks allow optimistic locking for read operations
    • ReentrantLocks are reentrant (StampedLocks are not)

    So if you have a scenario where you have contention (otherwise you may as well use synchronized or a simple Lock) and more readers than writers, using a StampedLock can significantly improve performance.

    However you should measure the performance based on your specific use case before jumping to conclusions.

    Heinz Kabutz has written about StampedLocks in his newsletter and he also made a presentation about performance.

提交回复
热议问题