Producer Consumer模块只输出null
Producer Consumer module just outputting null
我尝试制作一个生产者消费者 Java 程序,但它似乎没有工作:它只是没有终止,终端就这样保持:
我希望代码在新行上打印 Text 1 然后 Text 2 然后 Text 3。
我希望写入线程写入要打印的文本,然后读取线程实际打印文本。我的代码在下面。
我的另一个问题是什么时候会使用这种模型,目前作为初学者 Java 程序员是否值得学习。谢谢。代码如下:
public class Test {
public static void main(String[] args) {
Model model = new Model();
new Thread(new ModelThreadWriter(model)).start();
new Thread(new ModelThreadReader(model)).start();
}
}
class Model {
private String data;
private boolean isEmpty = true;
public synchronized void writeData(String data) {
while (!isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
this.data = data;
isEmpty = false;
notifyAll();
}
}
public synchronized String readData() {
while (isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
}
isEmpty = true;
notifyAll();
return data;
}
}
class ModelThreadWriter implements Runnable {
private Model model;
public ModelThreadWriter(Model model) {
this.model = model;
}
@Override
public void run() {
String[] text = {
"Text 1",
"Text 2",
"Text 3"
};
for (int i=0; i<text.length; i++) {
model.writeData(text[i]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
model.writeData("done");
}
}
class ModelThreadReader implements Runnable {
private Model model;
public ModelThreadReader(Model model) {
this.model = model;
}
@Override
public void run() {
for (String msg = model.readData(); !(msg == "done"); model.readData()) {
System.out.println(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
除了上面评论中提到的其他问题,您的 ModelThreadReader#run()
方法中的 for
循环永远不会终止。
for (String msg = model.readData(); !(msg == "done"); model.readData()) {
...
}
String msg
变量会在循环体第一次执行前被设置为调用model.readData()
的结果,之后就不会再设置了。除非第一次调用发生在 return "done"
上,否则循环不会终止。
也许你是故意的?
for (String msg=model.readData(); !(msg=="done");
msg=model.readData())...
您的代码中有几个错误。
1.In writeData
方法,this.data = data;isEmpty = false;notifyAll();
应该移出 while 循环。
2.In ModelThreadReader
, ==
=> equals
.
3.In ModelThreadReader
, for (String msg = model.readData(); !(msg == "done"); model.readData())
=> !(msg.equals("done")); msg = model.readData()
,否则每次都会打印出Text1
class Model {
private String data;
private boolean isEmpty = true;
public synchronized void writeData(String data) {
while (!isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
}
this.data = data;
isEmpty = false;
notifyAll();
}
public synchronized String readData() {
while (isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
}
isEmpty = true;
notifyAll();
return this.data;
}
}
class ModelThreadWriter implements Runnable {
private Model model;
public ModelThreadWriter(Model model) {
this.model = model;
}
@Override
public void run() {
String[] text = {
"Text 1",
"Text 2",
"Text 3"
};
for (int i=0; i<text.length; i++) {
model.writeData(text[i]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
model.writeData("done");
}
}
class ModelThreadReader implements Runnable {
private Model model;
public ModelThreadReader(Model model) {
this.model = model;
}
@Override
public void run() {
String msg = model.readData();
for (; !(msg.equals("done")); msg = model.readData()) {
System.out.println(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
我尝试制作一个生产者消费者 Java 程序,但它似乎没有工作:它只是没有终止,终端就这样保持:
public class Test {
public static void main(String[] args) {
Model model = new Model();
new Thread(new ModelThreadWriter(model)).start();
new Thread(new ModelThreadReader(model)).start();
}
}
class Model {
private String data;
private boolean isEmpty = true;
public synchronized void writeData(String data) {
while (!isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
this.data = data;
isEmpty = false;
notifyAll();
}
}
public synchronized String readData() {
while (isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
}
isEmpty = true;
notifyAll();
return data;
}
}
class ModelThreadWriter implements Runnable {
private Model model;
public ModelThreadWriter(Model model) {
this.model = model;
}
@Override
public void run() {
String[] text = {
"Text 1",
"Text 2",
"Text 3"
};
for (int i=0; i<text.length; i++) {
model.writeData(text[i]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
model.writeData("done");
}
}
class ModelThreadReader implements Runnable {
private Model model;
public ModelThreadReader(Model model) {
this.model = model;
}
@Override
public void run() {
for (String msg = model.readData(); !(msg == "done"); model.readData()) {
System.out.println(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
除了上面评论中提到的其他问题,您的 ModelThreadReader#run()
方法中的 for
循环永远不会终止。
for (String msg = model.readData(); !(msg == "done"); model.readData()) {
...
}
String msg
变量会在循环体第一次执行前被设置为调用model.readData()
的结果,之后就不会再设置了。除非第一次调用发生在 return "done"
上,否则循环不会终止。
也许你是故意的?
for (String msg=model.readData(); !(msg=="done");
msg=model.readData())...
您的代码中有几个错误。
1.In writeData
方法,this.data = data;isEmpty = false;notifyAll();
应该移出 while 循环。
2.In ModelThreadReader
, ==
=> equals
.
3.In ModelThreadReader
, for (String msg = model.readData(); !(msg == "done"); model.readData())
=> !(msg.equals("done")); msg = model.readData()
,否则每次都会打印出Text1
class Model {
private String data;
private boolean isEmpty = true;
public synchronized void writeData(String data) {
while (!isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
}
this.data = data;
isEmpty = false;
notifyAll();
}
public synchronized String readData() {
while (isEmpty) {
try {
wait();
} catch (InterruptedException e) {
}
}
isEmpty = true;
notifyAll();
return this.data;
}
}
class ModelThreadWriter implements Runnable {
private Model model;
public ModelThreadWriter(Model model) {
this.model = model;
}
@Override
public void run() {
String[] text = {
"Text 1",
"Text 2",
"Text 3"
};
for (int i=0; i<text.length; i++) {
model.writeData(text[i]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
model.writeData("done");
}
}
class ModelThreadReader implements Runnable {
private Model model;
public ModelThreadReader(Model model) {
this.model = model;
}
@Override
public void run() {
String msg = model.readData();
for (; !(msg.equals("done")); msg = model.readData()) {
System.out.println(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}