[英]Confused with custom read write lock implementation
I have seen some posts about the custom read-write lock
implementation in java
using wait/notify
. 我在使用wait/notify
java
看到了一些关于自定义read-write lock
实现的帖子。 It looks like: 看起来像:
public class ReadWriteLock{
private int readers;
private int writers;
private int writeRequests;
public synchronized void lockRead() throws InterruptedException{
while(writers > 0 || writeRequests > 0){
wait();
}
readers++;
}
public synchronized void unlockRead(){
readers--;
notifyAll();
}
public synchronized void lockWrite() throws InterruptedException{
writeRequests++;
while(readers > 0 || writers > 0){
wait();
}
writeRequests--;
writers++;
}
public synchronized void unlockWrite() throws InterruptedException{
writers--;
notifyAll();
}
}
I cannot comprehend how it could correctly work, unless I have not understood correctly how wait/notify
really works. 我无法理解它是如何正常工作的,除非我没有正确理解wait/notify
是如何工作的。 Assuming the read
requests and consequently Threads
are more, my questions are: 假设read
请求和Threads
更多,我的问题是:
If read Threads
acquire repeatedly the lock on the instance, how could a write Thread
increase the variable writeRequests
, since it can be increased only within a synchronized
method. 如果read Threads
重复获取实例上的锁定,则write Thread
如何增加变量writeRequests
,因为它只能在synchronized
方法中增加。 Hence a Thread
should acquire first the lock to do it (if I am not mistaken). 因此, Thread
应首先获取锁定(如果我没有记错的话)。 As long as a read Thread
calls wait
only if writeRequests
or writers
are greater than 0
, how can a write Thread
have the chance to acquire the lock? 只要read Thread
调用仅在writeRequests
或writers
大于0
wait
, write Thread
如何才有机会获取锁定?
Based on the above presumptions and statements, how could more than one read Threads
access a method at the same time, since they should first call lockRead()
which is synchronized as well? 基于上述假设和语句,多个read Threads
如何同时访问一个方法,因为它们应该首先调用同步的lockRead()
?
Edit : After seeing you edit to the question, you're asking what happens when multiple threads call wait()
inside the same synchronized blocks - see this for a detailed explanation on what is called 'releasing the monitor' - http://www.artima.com/insidejvm/ed2/threadsynchP.html 编辑:在看到你编辑问题之后,你问的是当多个线程在同一个同步块中调用wait()
时会发生什么 - 请参阅此内容以获取有关所谓“释放监视器”的详细说明 - http:// www .artima.com / insidejvm / ED2 / threadsynchP.html
To simplify things: 为了简化事情:
synchronized(this)
blocks. 同步方法类似于synchronized(this)
块。 wait()
inside synchronized
blocks release the lock and switches the thread to WAITING state. 在synchronized
块中调用wait()
释放锁并将线程切换到WAITING状态。 in this scenario other threads can acquire the lock on the same object and possibly notify the other waiting threads on state change (your unlock methods demonstrate that) by using the same object waited on ( this
in our case, because you're using synchronized methods) 在这种情况下其他线程可以使用同一个对象获取同一对象的锁,并有可能通知状态改变其他等待的线程(解锁方法证明)上(等待this
在我们的情况下,因为你使用synchronized方法) unlockRead()
/ unlockWrite()
) - meaning they can block on mutual exclusion upon entry, but don't run any blocking code(and end swiftly). 如果您根据该原则映射调用每个方法的可能方案,您可以看到方法是非等待的( unlockRead()
/ unlockWrite()
) - 这意味着它们可以在进入时阻止互斥,但不运行任何方法阻止代码(并迅速结束)。 Or, they are waiting but non-blocking ( lockRead()
/ lockWrite()
) - Just like the unlock methods with the addition their execution could potentially be stalled, however they don't block, but rather wait in such scenarios. 或者,他们正在等待但是非阻塞( lockRead()
/ lockWrite()
) - 就像解锁方法一样,他们的执行可能会被阻止,但是他们不会阻止,而是在这种情况下等待。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.