3

我正在用 Java 中的监视器实现读者作者问题。有很多读者和作家。当一个作家正在写作时,没有其他读者或作家可以阅读或写作。许多读者可以同时阅读。我不知道这段代码有什么问题。有死锁问题。

class Monitor {
    private int readers; // specifies number of readers reading
    private boolean writing; // specifies if someone is writing
    private Condition OK_to_Read, OK_to_Write;

    public Monitor() {
        readers = 0;
        writing = false;
        OK_to_Read = new Condition();
        OK_to_Write = new Condition();
    }

    public synchronized void Start_Read(int n) {

        System.out.println("wants to read " + n);
        if (writing || OK_to_Write.is_non_empty()) {
            try {
                System.out.println("reader is waiting " + n);
                OK_to_Read.wait_();
            } catch (InterruptedException e) {
            }
        }
        readers += 1;
        OK_to_Read.release_all();

    }

    public synchronized void End_Read(int n) {

        System.out.println("finished reading " + n);
        readers -= 1;

        if (OK_to_Write.is_non_empty()) {
            OK_to_Write.release_one();
        } else if (OK_to_Read.is_non_empty()) {
            OK_to_Read.release_one();
        } else {
            OK_to_Write.release_all();
        }

    }

    public synchronized void Start_Write(int n) {
        System.out.println("wants to write " + n);
        if (readers != 0 || writing) {
            try {
                System.out.println("Writer is waiting " + n);
                OK_to_Write.wait_();
            } catch (InterruptedException e) {
            }
        }

        writing = true;

    }

    public synchronized void End_Write(int n) {

        System.out.println("finished writing " + n);
        writing = false;
        if (OK_to_Read.is_non_empty()) {
            OK_to_Read.release_one();
        } else if (OK_to_Write.is_non_empty()) {
            OK_to_Write.release_one();
        } else {
            OK_to_Read.release_all();
        }

    }

}

class Condition {
    private int number;// specifies the number of readers/writers waiting

    public Condition() {
        number = 0;
    }

    public synchronized boolean is_non_empty() {
        if (number == 0)
            return false;
        else
            return true;
    }

    public synchronized void release_all() {
        number = 0;
        notifyAll();
    }

    public synchronized void release_one() {
        number -= 1;
        notify();
    }

    public synchronized void wait_() throws InterruptedException {
        number++;
        wait();
    }

}

class Reader extends Thread {
    private Monitor M;
    private String value;

    public Reader(String name, Monitor c) {
        super(name);
        M = c;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            M.Start_Read(i);
            // System.out.println("Reader "+getName()+" is retreiving data...");
            System.out.println("Reader is reading " + i);
            M.End_Read(i);
        }

    }
}

class Writer extends Thread {
    private Monitor M;
    private int value;

    public Writer(String name, Monitor d) {
        super(name);
        M = d;
    }

    public void run() {
        for (int j = 0; j < 10; j++) {
            M.Start_Write(j);
            // System.out.println("Writer "+getName()+" is writing data...");
            System.out.println("Writer is writing " + j);
            M.End_Write(j);
        }

    }
}

class mainClass {
    public static void main(String[] args) {
        Monitor M = new Monitor();
        Reader reader = new Reader("1", M);
        Writer writer = new Writer("1", M);
        writer.start();
        reader.start();
    }
}
4

3 回答 3

2

问题其实很简单:你所有的Start_Write, End_Write, Start_Read,End_Read方法都标记为synchronized. 您的程序中只有一个实例Monitor。可以将synchronized其视为默认附加到每个 java 对象的排他锁。一次只能有一个线程在属于该对象的任何同步方法中运行。仅当方法返回时才释放锁。

考虑以下事件序列:

1. Writer enters Start_Write, and takes the lock on Monitor
2. Writer exits Start_Write, and releases the lock on Monitor
3. Reader enters Start_Read, and takes the lock on Monitor
4. Reader cannot exit Start_Read, because the writer is still writing.
   The lock on Monitor IS NOT RELEASED
5. Writer wants to enter End_Write, but the lock is not available because
   Reader is still holding it

这是你的僵局。

  • Reader 不能在不退出 Start_Read 的情况下释放锁
  • 要退出 Start_Read,它会等待 Writer 调用 End_Write
  • Writer 无法调用 End_Write,因为它需要锁才能这样做

您的问题的解决方案非常简单:放弃所有这些ReentrantReadWriteLock由 JDK 提供且专门用于处理您的问题的自定义逻辑和使用。

ReetrantReadWriteLock.readLock().lock()等同于Monitor.Start_Read() ReetrantReadWriteLock.readLock().unlock()等同于Monitor.End_Read() ReetrantReadWriteLock.writeLock().lock()等同于Monitor.Start_Write() ReetrantReadWriteLock.writeLock().unlock()等同于Monitor.End_Write()

还有一条评论:您应该始终将释放锁的代码放入一个finally块中,以确保在抛出异常的情况下您的应用程序不会陷入死锁。例如:

class Writer extends Thread {
    private ReentrantReadWriteLock lock;
    private int value;

    public Writer(String name, ReentrantReadWriteLock lock) {
        super(name);
        this.lock = lock;
    }

    public void run() {
        for (int j = 0; j < 10; j++) {
            lock.writeLock().lock();
            try{
                // System.out.println("Writer "+getName()+" is writing data...");
                System.out.println("Writer is writing " + j);
            } finally {
                lock.writeLock().unlock();
            }
        }

    }
}

于 2019-09-11T13:04:07.883 回答
-1

我已将sleep方法放入您的代码中。请检查并尝试。

class Monitor
{
private volatile int readers; //specifies number of readers reading
private volatile boolean writing; //specifies if someone is writing
private volatile Condition OK_to_Read, OK_to_Write;

 public Monitor()
{
    readers = 0;
    writing = false;
    OK_to_Read = new Condition();
    OK_to_Write = new Condition();
}

public synchronized void Start_Read(int n)
{

     System.out.println("wants to read " + n);
    if(writing || OK_to_Write.is_non_empty())
    {
        try{
            System.out.println("reader is waiting " + n);
            OK_to_Read.sleep_();
        }
        catch(InterruptedException e){}
    }
    readers += 1;
    OK_to_Read.release_all();

}

public synchronized void End_Read(int n)
{

        System.out.println("finished reading " + n);
        readers -= 1;

        if(OK_to_Write.is_non_empty())
        {
            OK_to_Write.release_one();
        }
        else if(OK_to_Read.is_non_empty())
        {
            OK_to_Read.release_one();
        }
        else
        {
            OK_to_Write.release_all();
        }

}

public synchronized void Start_Write(int n)
{
    System.out.println("wants to write " + n);
    if(readers != 0 || writing)
    {
        try{
            System.out.println("Writer is waiting " + n);
            OK_to_Write.sleep_();
                }catch(InterruptedException e){}
    }

    writing = true;

}

public synchronized void End_Write(int n)
{

    System.out.println("finished writing " + n);
    writing = false;
    if(OK_to_Read.is_non_empty())
    {
        OK_to_Read.release_one();
    }
    else if(OK_to_Write.is_non_empty())
    {
        OK_to_Write.release_one();
    }
    else
    {
        OK_to_Read.release_all();
    }

}

}

class Condition
{
private int number;//specifies the number of readers/writers waiting

public Condition()
{ 
    number = 0; 
}

public synchronized boolean is_non_empty()  
{ 
    if(number == 0)
        return false; 
    else
        return true;
}

public synchronized void release_all()
{ 
number = 0;
notifyAll(); 
}


public synchronized void release_one()
{ 
number -=1;
notify(); 
}   

public synchronized void wait_() throws InterruptedException
{  
    number++;
    wait();
}
public synchronized void sleep_() throws InterruptedException
{  
    Thread.sleep(1000);
}

}


class Reader extends Thread
{
private Monitor M;
private String value;
public Reader(String name,Monitor c)
{
    super(name);
    M=c;
}

public void run()
{
    for(int i = 0; i < 10; i++){
            M.Start_Read(i);
            //System.out.println("Reader "+getName()+" is retreiving data...");
            System.out.println("Reader is reading " + i);
            M.End_Read(i);
    }

}
}

class Writer extends Thread
{
private Monitor M;
private int value;
public Writer(String name, Monitor d)
{
    super(name);
    M = d;
}

public void run()
{
    for(int j = 0; j < 10; j++){
            M.Start_Write(j);
            //System.out.println("Writer "+getName()+" is writing data...");
            System.out.println("Writer is writing " + j);
            M.End_Write(j);
    }

}
}

public class Demo
{
public static void main(String [] args)
{
    Monitor M = new Monitor();
    Reader reader = new Reader("1",M);
    Writer writer = new Writer("1",M);
    writer.start();
    reader.start();     
} }
于 2015-09-29T07:55:35.957 回答
-1

您应该为 wait() 函数设置一个值。

等待 10 秒:

public synchronized void wait_() throws InterruptedException {
        number++;
        wait(10000);
    }

对我来说,修改后它工作正常。

于 2016-06-18T21:16:58.333 回答