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

时间:2018-01-30 15:36:07

标签: java multithreading concurrency semaphore readerwriterlock

最近我一直试图绕过并发。目前我正在尝试为ReaderWriter问题找到解决方案。

我有一个类文件,它计算读者/作者的数量,并有两个信号量。

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

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

由于某些原因,我无法弄清楚Writers没有编辑类File中的String文件。

提前致谢:)

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();
    }

}}

1 个答案:

答案 0 :(得分:0)

编辑:

Thread.currentThread().wait()是错误的。 在Java文档中不建议等待线程。

谢谢@JB Nizet帮助我。