简体   繁体   English

ReentrantReadWriteLock vs synchronized

[英]ReentrantReadWriteLock vs synchronized

When should we use ReentrantReadWriteLock as compared to synchronized keyword in multithreaded environment in Java? 我们应该何时使用ReentrantReadWriteLock与Java中多线程环境中的synchronized关键字进行比较?

What are the benefits of using ReentrantReadWriteLock over synchronized in Java? 使用ReentrantReadWriteLock而不是Java中的同步有什么好处?

Can any one give an example as well (in Java)? 任何人都可以给出一个例子(在Java中)吗?

Thanks! 谢谢!

Synchronized allows in one thread at a time. 同步允许一次在一个线程中。

Read/Write locks allow in multiple readers a the same time, but only if no writers are already in. Hence under some usage scenarios we can get better concurrency, because the reader populations can proceed together. 读/写锁允许同时在多个读取器中,但只有在没有编写器的情况下才允许。因此,在某些使用场景下,我们可以获得更好的并发性,因为读者群可以一起进行。

Java API documentation gives the example of collection classes which are expected to have more readers than writers. Java API 文档给出了集合类的示例,这些集合类应该具有比编写器更多的读者。

The locking article by Brian explains in detail the pros and cons of each approach. Brian锁定文章详细解释了每种方法的优缺点。

The Lock framework is a compatible replacement for synchronization, which offers many features not provided by synchronized, as well as implementations offering better performance under contention. Lock框架是同步的兼容替代品,它提供了许多未由synchronized提供的功能,以及在争用下提供更好性能的实现。 However, the existence of these obvious benefits are not a good enough reason to always prefer ReentrantLock to synchronized. 然而,这些明显的好处的存在并不足以让ReentrantLock更加同步。 Instead, make the decision on the basis of whether you need the power of ReentrantLock. 相反,根据您是否需要ReentrantLock的强大功能做出决定。 In the vast majority of cases, you will not -- synchronization works just fine, works on all JVMs, is understood by a wider range of developers, and is less error-prone. 在绝大多数情况下,您不会 - 同步工作正常,适用于所有JVM,可以被更广泛的开发人员理解,并且不易出错。 Save Lock for when you really need it. 在您真正需要时保存锁定。 In those cases, you'll be glad you have it. 在那些情况下,你会很高兴你拥有它。

It should be noted that StampedLock has since come out with Java 8, and it's much faster than ReentrantReadWriteLock (especially as you use more and more threads) when you don't use the lock in a reentrant manner (using StampedLock reentrantly can lead to deadlocks, so don't do it). 应该注意的是, StampedLock已经推出了Java 8,并且它比ReentrantReadWriteLock快得多(特别是当你使用越来越多的线程时)你不以可重入的方式使用锁(使用StampedLock可能会导致死锁) ,所以不要这样做)。

It also allows optimistic read nonlocks that are available if there is no write lock in effect. 它还允许在没有写锁定的情况下可用的乐观读取非锁定。 Unlike a normal read lock, they don't block a write lock from being established. 与普通读锁不同,它们不会阻止建立写锁。 You can check whether a write lock has been established over your optimistic read nonlock by using the validate method . 您可以使用validate方法检查是否已在乐观读取非锁定上建立了写锁定。

Its interface is a little different since you have to store a long value called a stamp in order to later unlock a read or write lock properly or to later validate an optimistic read nonlock properly when you're done. 它的接口有点不同,因为你必须存储一个称为戳记的long值,以便以后正确地解锁读或写锁,或者在你完成后再适当地validate乐观读取非锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM