使用Monitor的Writer / Reader无法正常工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Monitor的Writer / Reader无法正常工作相关的知识,希望对你有一定的参考价值。
我正在执行一个任务,在那里实现一个使用监视器进行同步的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();
}
}
}
您可以尝试此解决方案,只需在while块之外为'hasString'赋值。
/**
* 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;
}
代码现在可以工作(在@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;
}
以上是关于使用Monitor的Writer / Reader无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
对字符串数据使用 TextReader/Writer 而不是 Binary Reader/Writer 有啥好处?
JavaI/O:简单的使用Reader和Writer来操作文件