使用Monitor的Writer / Reader无法正常工作

时间:2017-12-25 11:51:59

标签: java multithreading synchronization monitor producer-consumer

我正在做一个正在实现使用监视器进行同步的Writer / Reader问题的任务。

场景:编写器应该一次从列表中写入一个字符串到缓冲区(缓冲区一次只能容纳一个字符串)。然后,读者应该从缓冲区中读取字符串并在gui中打印字符串,并在打印件之间暂停0.5秒。该程序使用一个单一的编写器线程和一个单一的读取器线程,当按下gui中的按钮时,它们同时启动。

我的问题:我无法使同步工作,读者只需每0.5秒输出一次null(这意味着当读者尝试从中读取时,缓冲区很可能为空)。我设法找到了一个错误:作者没有到达try buffer.put(text.get(i)),尽管我无法弄清楚原因。

任何可以提供帮助的人?非常感激!我的代码在下面,希望它足够了,我不想垃圾邮件太多代码。 (我对Writer / Reader问题也很新,但已阅读了一些编码示例等。)

作家:

public void run() {
    buffer.length(text.size());
    for (int i = 0; i < text.size(); i++) { //loops thorugh the list of strings
        System.out.println("hj"); //reaches this
        try {
            buffer.put(text.get(i)); //put the string into the buffer
            System.out.println("put done"); // never reaches this
            sleep(); //pauses the thread for 0.5 seconds
        } catch (InterruptedException e) {
            System.out.println("error"); //never reaches this either
            e.printStackTrace();
        }
    }
}

缓冲液:

/**
 * gets a string from the writer
 * @throws InterruptedException 
 */
public synchronized void put(String string) throws InterruptedException {
    while(hasString == false) { //we only want to put a string into the buffer if it is empty
        wait();
        this.string = string;
        hasString = true;
        notify();
    }
}

/**
 * passes on a string to the reader
 * @throws InterruptedException 
 */
public synchronized String get() throws InterruptedException {
    while(hasString == true) { //we only want to pass on a string if the buffer is NOT empty
        wait();
        hasString = false;
        notify();
    }
    return string;
}

阅读器:

/**
 * reads a string from the buffer and writes it to the gui in the destination-tab
 */
public void run() {
    for (int i  = 0; i < buffer.getLength(); i++) { //loops through the length of the list with strings
        try {
            string = buffer.get(); //gets string from buffer
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        //writes the string in the gui
        sb.append(string);
        GUIMonitor.txtPaneDest.setText(sb.toString());
        try {
            sleep(); //pauses the thread for 0.5 seconds
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以尝试此解决方案,只需将值分配给&#39; hasString&#39;在街区之外。

 /**
 * gets a string from the writer
 * @throws InterruptedException 
 */
public synchronized void put(String string) throws InterruptedException {
    while(hasString == false) { //we only want to put a string into the buffer if it is empty
        wait();
    }
    this.string = string;
    hasString = true;
    notify();

}

/**
 * passes on a string to the reader
 * @throws InterruptedException 
 */
public synchronized String get() throws InterruptedException {
    while(hasString == true) { //we only want to pass on a string if the buffer is NOT empty
        wait();
    }
    hasString = false;
    notify();
    return string;
}

答案 1 :(得分:0)

代码现在可以工作(在@whaat回答的帮助下),只更改Buffer-class中的代码,同时保持Writer和Reader不变。我会留下缓冲区的代码以防万一其他人遇到与我一样的问题:)

/**
 * gets a string from the writer
 * @throws InterruptedException 
 */
public synchronized void put(String string) throws InterruptedException {
    while(hasString == true) { //we only want to put a string into the buffer if it is empty
        wait();

    }
    this.string = string;
    hasString = true;
    notify();
}

/**
 * passes on a string to the reader
 * @throws InterruptedException 
 */
public synchronized String get() throws InterruptedException {
    while(hasString == false) { //we only want to pass on a string if the buffer is NOT empty
        wait();
    }
    hasString = false;
    notify();
    return string;
}