简体   繁体   English

混淆了自定义读写锁实现

[英]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调用仅在writeRequestswriters大于0 waitwrite 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 methods are like synchronized(this) blocks. 同步方法类似于synchronized(this)块。
  • calling 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方法)
  • If you map the possible scenarios for calling each method according to that priniciple you can see that methods are either non-waiting ( 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() ) - 就像解锁方法一样,他们的执行可能会被阻止,但是他们不会阻止,而是在这种情况下等待。
  • So in any case you can consider your code as non-blocking and therefor it doesn't pose any real issue ( at least none that I can see ). 因此,无论如何,您可以将您的代码视为非阻塞,因此它不会造成任何实际问题(至少没有我能看到的)。
  • That said, you should protect against unlocking non-existent locks, causes that'll cause an undesired behavior where counters would go below 0 ( which would in turn affect the lock methods) 也就是说,你应该防止解锁不存在的锁,这会导致一个不希望的行为,其中计数器将低于0(这反过来会影响锁定方法)

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

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