简体   繁体   English

有关读写锁的查询

[英]Query regarding read-write locks

I am delving on the java concurrent APIs and trying to understand the usefulness of read-write locks. 我正在研究java并发API并尝试理解读写锁的有用性。 The javadoc says that a readwrite block maintains a pair of locks, one for read and the other for write operations. javadoc表示readwrite块维护一对锁,一个用于读取,另一个用于写操作。 While the write lock is exclusive access by a thread, multiple threads can acquire the read lock. 虽然写锁是线程的独占访问,但多个线程可以获取读锁。 So if in the read section all we are doing is read operations and we are anyways providing multiple threads access, what is the need to have the readlock in the first place ? 因此,如果在读取部分中我们所做的只是读取操作,并且我们无论如何都提供多线程访问,那么首先需要读取锁定是什么? Is there a scenario when the readwrite lock is actually useful? 是否存在readwrite锁实际有用的情况?

If you're reading the data in many threads you're likely not to see the latest changes to the data due to visibility issues. 如果您在许多线程中读取数据,由于可见性问题,您可能无法看到数据的最新更改。 There are many layers where data can be cached on per-thread basis: diffent layers of CPU cache, RAM access buffers and so on. 有许多层可以在每个线程的基础上缓存数据:不同的CPU缓存层,RAM访问缓冲区等等。 Havling read lock in place you may be sure, that you're always observing the latest state. Havling读取锁定你可能确定,你总是在观察最新的状态。

Write locks are even stronger, providing an atomicity of access along with the visibility of the latest changes. 写锁更强大,提供访问的原子性以及最新更改的可见性。

The main reason to have different kind of locks here is to have an ability to obtain sufficient level of synchronization without introducing too much overhead and locks for other threads. 这里有不同类型锁的主要原因是能够获得足够的同步水平,而不会为其他线程引入太多开销和锁。

Is there a scenario when the readwrite lock is actually useful? 是否存在readwrite锁实际有用的情况?

It's highly useful when you have some in-memory data (Array, Collection or whatever), which is queried a lot by different threads, but the updates of this data happens quite rarely. 当你有一些内存数据(数组,集合或其他)时非常有用,不同的线程会对它们进行大量查询,但这些数据的更新很少发生。 In this case having separate locks (read lock for queries and write lock for updates) may give you a significant performace benefit. 在这种情况下,具有单独的锁(用于查询的读锁和用于更新的写锁)可以为您提供显着的性能优势。

.... what is the need to have the readlock in the first place ....首先需要使用readlock是什么

While reading, you need to prevent a writer acquiring the lock ... until all readers have finished. 在阅读时,你需要阻止作家获得锁...直到所有读者都完成。 But it is OK for another reader to acquire the lock. 但是其他读者可以获得锁定。

While writing, you need to prevent a reader acquiring the lock ... until the writer has finished. 在写作时,你需要阻止读者获取锁...直到作者完成。

(In other words, there can be one writer, OR multiple readers holding locks ... but not both.) (换句话说,可以有一个作家,或者多个读者持有锁......但不是两个。)

For the purposes of describing this, it is helpful to describe the behaviour as being two locks. 出于描述的目的,将行为描述为两个锁是有帮助的。 What actually happens under the hood ... is implementation specific. 实际发生的事情......是特定于实现的。


Is there a scenario when the readwrite lock is actually useful? 是否存在readwrite锁实际有用的情况?

Well yea. 好吧,是的。 In any scenario where you can distinguish between threads that require shared read-only access and those that require exclusive read-write (or write-only), a ReadWrite lock allows more concurrency than a simple Lock or a primitive mutex. 在任何可以区分需要共享只读访问的线程和需要独占读写(或只写)的线程的情况下, ReadWrite锁允许比简单的Lock或原始互斥锁更多的并发性。

The reason for having a read-only lock is that if some other thread has an object locked for writing, that object's state may be inconsistent, and so you don't want to start reading it with no lock at all. 拥有只读锁的原因是,如果某个其他线程的对象被锁定以进行写入,则该对象的状态可能不一致,因此您根本不想开始读取它而没有锁定。 Obtaining a read lock ensures that (1) the object is in a consistent state while you're looking at it because no other thread is modifying it and (2) no other thread can start modifying it until you're through looking at it. 获取读锁定可确保(1)当您查看对象时,对象处于一致状态,因为没有其他线程正在修改它,并且(2)在您查看它之前,没有其他线程可以开始修改它。 We have the read lock as a separate type because if it's usual for lots of threads to read but to have few updates, then the readers can all be looking at the same time. 我们将读锁定作为一个单独的类型,因为如果通常需要读取大量线程但很少有更新,那么读者可以同时查看。

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

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