为什么我的 ReaderWriter 解决方案不起作用? (java, 并发)

Why doesnt my ReaderWriter solution work ? (java, concurrency)

所以最近我一直在努力研究并发性。目前我正在尝试为 ReaderWriter 问题找到解决方案。

我有一个 class 文件,它计算 Readers/Writers 的数量并且有两个信号量。

当 Reader 尝试读取时,只要有 Writer Thread 正在写入,它就必须等待。当它进入 readCount 在 readerSemaphore 内递增 & 离开时在同一个信号量内递减。

当 Writer 试图进入时,只要有多个 reader,它就必须等待。当它进入时,它会获取 writerSemaphore 并增加 writerCount。当它离开时释放信号量。

出于某种原因,我无法弄清楚作者没有编辑 class 文件中的字符串文件。

提前致谢:)

public class Main { 
public static void main(String[]args) {

    File file = new File("1. Chapter: ");

    Writer w1 = new Writer(file, " w1 ");
    Writer w2 = new Writer(file, " w2 ");

    Reader r1 = new Reader(file);
    Reader r2 = new Reader(file);
    Reader r3 = new Reader(file);
    Reader r4 = new Reader(file);
    Reader r5 = new Reader(file);

    w1.start();
    w2.start();

    r1.start();
    r2.start();
    r3.start();
    r4.start();
    r5.start();     

    try {
        w2.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("-> Final output: " + file.file);
}}


public class File { 
public String file;

private int readCount;
private int writeCount;

private Semaphore semReader;
private Semaphore semWriter;

public File(String file) {
    this.file = file;

    readCount = 0;
    writeCount = 0;

    semReader = new Semaphore(1);
    semWriter = new Semaphore(1);
}

public synchronized void startReading() {

    try {

        while(writeCount == 1) {
            Thread.currentThread().wait();
        }

        semReader.acquire();
        readCount++;
        semReader.release();

        System.out.println(" --- File was read");

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public synchronized String endReading() {

    String temp = file;

    try {   

        semReader.acquire();    
        readCount--;    
        semReader.release();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return temp;
}

public synchronized void startWriting(String edit) {

    try {

        while(readCount > 0) {
            Thread.currentThread().wait();
        }

        semWriter.acquire();
        writeCount++;   

        System.out.println(" --- File got edited");
        file = file + "hi";
    }
    catch (Exception e) {
    }   
}

public synchronized void endWriting() {

    writeCount--;
    semWriter.release();
}}




public class Writer extends Thread { 
private File file;
private String edit;

public Writer(File file, String edit) {
    this.file = file;
    this.edit = edit;
}

@Override
public void run() {

    Random rand = new Random();

    try {

        sleep(1000);
        System.out.println(">W: " + Thread.currentThread().getName() + " started first write.");
        file.startWriting(" first" + edit);

        sleep(3000);
        System.out.println(">W: " + Thread.currentThread().getName() + " ended first write.");
        file.endWriting();

        sleep(2000);
        System.out.println(">W: " + Thread.currentThread().getName() + " started second write.");
        file.startWriting(" second" + edit);

        sleep(3000);
        System.out.println(">W: " + Thread.currentThread().getName() + " ended second write.");
        file.endWriting();

        System.out.println(">W: " + Thread.currentThread().getName() + " finished");

    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}}




public class Reader extends Thread { 
private File file;

public Reader(File file) {
    this.file = file;
}

@Override
public void run() {

    Random rand = new Random();

    try {

        sleep(rand.nextInt(2000));
        System.out.println(">R: " + Thread.currentThread().getName() + " startet first read.");
        file.startReading();

        sleep(3000);
        System.out.print(">R: " + Thread.currentThread().getName() + " ended first read: ");
        System.out.println(file.endReading());

        sleep(rand.nextInt(2000));
        System.out.println(">R: " + Thread.currentThread().getName() + " startet second read.");            
        file.startReading();

        sleep(3000);
        System.out.print(">R: " + Thread.currentThread().getName() + " ended second read: ");   
        System.out.println(file.endReading());

        System.out.println(">R: " + Thread.currentThread().getName() + " finished");

    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}}

编辑:

Thread.currentThread().wait() 错了。 java 文档不鼓励等待线程。

谢谢@JB Nizet 帮助我。