gpt4 book ai didi

java - ReadWriteLock 与 StampedLock

转载 作者:行者123 更新时间:2023-11-30 06:15:23 29 4
gpt4 key购买 nike

我一直在使用 ReadWriteLock `s 来实现/维护锁定习语。

自 JDK8 StampedLock已介绍。由于 RWLocks 以其缓慢和糟糕的性能而闻名,StampedLock 看起来像是一个替代品(它们不可重入,所以快得多)。

然而,除了性能,在我看来,StampedLock 的维护和使用要困难得多,也复杂得多 - 例如线程现在可以针对自身死锁 - 因此应采取相应的措施。

与 RWLock 相比,StampedLock 有哪些优势?

最佳答案

article详细解释差异。

The ReentrantReadWriteLock had a lot of shortcomings: It suffered from starvation. You could not upgrade a read lock into a write lock. There was no support for optimistic reads. Programmers "in the know" mostly avoided using them.

Doug Lea's new Java 8 StampedLock addresses all these shortcomings. With some clever code idioms we can also get better performance.

关于java - ReadWriteLock 与 StampedLock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918004/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com