简体   繁体   English

在对象的方法内部调用wait方法时会发生什么?

[英]What happens when the wait method is called inside the method of an object?

I have method like the following in the main thread, that invokes a method on my data structure as follows -: 我在主线程中有如下方法,该方法在我的数据结构上调用如下方法:

public static void main(String[] args){

data_structure_object.insert(value);
}

and I am using a ReadWrite object call it rwLock inside the data structure class that is used to prevent thread interference, the Read Write class looks like the following -: 并且我在用于防止线程干扰的数据结构类中使用一个ReadWrite对象将其称为rwLock,Read Write类如下所示:

public class ReadWriteLocks {

    // these 3 variables help in creating a read write lock
    private int numberOfReaders = 0;
    private int numberOfWriters = 0;
    private int numberOfWriteRequests = 0;

    // getter method for the number of readers
    public int getNumberOfReaders() {
        return this.numberOfReaders;
    }

    // getter method for the number of writers
    public int getNumberOfWriters() {
        return this.numberOfWriters;
    }

    // getter method for the number of write requests
    public int getNumberOfWriteRequests() {
        return this.numberOfWriteRequests;
    }

    // this function checks if a thread can acquire the lock
    public synchronized void lockRead() throws InterruptedException {

        while (numberOfWriters > 0 || numberOfWriteRequests > 0)
            this.wait();
    }

    // this function unlocks a lock occupied by a reader thread
    public synchronized void unlockRead() {

        // decrement the number of readers
        --numberOfReaders;
        notifyAll();
    }

    // this function checks if a thread can acquire the write lock
    public synchronized void lockWrite() throws InterruptedException {

        // increase the number of write requests
        ++numberOfWriteRequests;

        while (numberOfReaders > 0 || numberOfWriters > 0)
            this.wait();

        --numberOfWriteRequests;
        ++numberOfWriters;
    }

    // this function is used to take a thread away from the lock
    public synchronized void unlockWrite() {

        // decrement the number of writers
        --numberOfWriters;

        // notify all the threads
        this.notifyAll();
    }

}

and inside the insert method of the data structure, I include the following code snippet 在数据结构的插入方法中,我包含以下代码片段

// acquire the read/write lock
        try {
            rwLock.lockRead();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Some operation

        // release the lock
        rwLock.unlockRead();

The question is, Is this a valid way of ensuring fairness and also locking threads so as to maintain the data structure consistency? 问题是,这是确保公平并锁定线程以保持数据结构一致性的有效方法吗? also besides all this I am not able to figure out how shall I provide the following functionality -: "Allowing multiple Readers to acquire the lock and reading the data till there is no writer requesting or writing to the resource" , I am pretty confused about the situation kindly help. 除了所有这些,我还无法弄清楚如何提供以下功能-:“允许多个读取器获取锁并读取数据,直到没有任何写入器请求或写入资源为止”,我对此感到非常困惑情况会有所帮助。

Apart from the fact that you are attempting to duplicate a perfectly good existing ReadWriteLock posing as a ReentrantReadWriteLock there are two certain issues in your code: 除了您试图复制一个完美的现有ReadWriteLock冒充ReentrantReadWriteLock这一事实外,您的代码中还有两个特定问题:

  1. Your instance variables should be volatile . 您的实例变量应该是volatile

     private volatile int numberOfReaders = 0; private volatile int numberOfWriters = 0; private volatile int numberOfWriteRequests = 0; 
  2. You need to be careful with your transition from lock-requested to locked. 从锁定请求到锁定的过渡需要谨慎。

     --numberOfWriteRequests; ++numberOfWriters; 

probably should be 大概应该是

    ++numberOfWriters;
    --numberOfWriteRequests;

because there could be a moment between those two instructions when numberOfWriteRequests is zero and numberOfWriters is zero. 因为当numberOfWriteRequests为零而numberOfWriters为零时,在这两条指令之间可能会有片刻。 This would let your spin loop in lockRead out and things would break ... sometimes. 这会使您的自旋循环进入lockRead出状态,有时会断裂。

You would probably be best to get this moved to code review. 您可能最好将其移至代码审查。

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

相关问题 在方法中两次调用递归时会发生什么? - What happens when recursion is called twice in a method? 一旦调用setException方法,对Settable Future对象会发生什么? - What happens to a Settable Future object once the setException method is called on it? 当一个线程通过另一个类调用synchronized方法时会发生什么? - What happens when a synchronized method is called by a thread via another class? 当您在方法内部更改引用时会发生什么? (7) - What happens when you change a reference inside a method? (7) 在Java中调用方法后会发生什么 - What happens after a method is called in Java 当使用空对象引用调用静态方法时会发生什么? - What happens when a static method is invoked using a null object reference? 将子类对象传递给具有超类参数的方法时会发生什么? - What happens when a subclass object is passed to a method with a superclass parameter? 将.method放在对象之后会发生什么? - What happens when you put a .method after an object? 当原始resultSet从方法返回到新对象时会发生什么? - What happens to the original resultSet when it is returned from a method into a new object? 当我自己用参数字符串抛出异常时究竟会发生什么? 什么时候调用 toString 方法? - What exactly happens when I throw an exception myself with a parameter string? When does the toString method get called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM