这个程序是教材上的 刚刚开始学习生产者消费者问题
看着程序也没什么问题 为什么一运行就不但没有结果 而且无限循环
只有一个“Kellerman生产一个产品”
求教 !public class ProducterConsumer { public static void main(String[] args) {
Operation p=new Operation();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}}class Operation {
String name = "";
boolean bfull = false; public synchronized void put(String name) {
this.name = name;
if (bfull)
try {
wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(name + "生产一个产品");
bfull = true;
notify();
} public synchronized void get() {
if (!bfull) {
try {
wait();
} catch (InterruptedException e) {
}
System.out.println("顾客取走一个产品");
bfull = false;
notify();
}
}
}class Producer implements Runnable {
Operation p; public Producer(Operation p) {
this.p = p;
} public void run() {
int i = 0;
while (true) {
if (i == 0)
p.put("Kellerman");
else
p.put("Kim");
i = (i + 1) % 2;
}
}
}class Consumer implements Runnable {
Operation p; public Consumer(Operation p) {
this.p = p;
} public void run() {
while (true) {
p.get();
}
}
}
看着程序也没什么问题 为什么一运行就不但没有结果 而且无限循环
只有一个“Kellerman生产一个产品”
求教 !public class ProducterConsumer { public static void main(String[] args) {
Operation p=new Operation();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}}class Operation {
String name = "";
boolean bfull = false; public synchronized void put(String name) {
this.name = name;
if (bfull)
try {
wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(name + "生产一个产品");
bfull = true;
notify();
} public synchronized void get() {
if (!bfull) {
try {
wait();
} catch (InterruptedException e) {
}
System.out.println("顾客取走一个产品");
bfull = false;
notify();
}
}
}class Producer implements Runnable {
Operation p; public Producer(Operation p) {
this.p = p;
} public void run() {
int i = 0;
while (true) {
if (i == 0)
p.put("Kellerman");
else
p.put("Kim");
i = (i + 1) % 2;
}
}
}class Consumer implements Runnable {
Operation p; public Consumer(Operation p) {
this.p = p;
} public void run() {
while (true) {
p.get();
}
}
}
if (!bfull) {
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println("顾客取走一个产品");
bfull = false;
notify();
}
String name = "";
boolean bfull = false; public synchronized void put(String name) {
if (!bfull){
this.name = name;
System.out.println(name + "生产一个产品");
bfull = true;
notify();
}
try {
wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
}
} public synchronized void get() {
if (!bfull) {
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println("顾客取走一个产品");
bfull = false;
notify();
}
}
只能通过知识和经验去人工分析代码如楼上所说,get方法写错了
因为如果get调用前,已经 bfull为true的话,get就直接返回了,啥也没拿到
而put的,因为阻塞,也只能生产一个出来
并且在被唤醒后,再次检查条件是否满足。当然,一般情况下要用到循环等待
class Operation {
String name = "";
boolean bfull = false; public synchronized void put(String name) {
while (bfull) {
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println(name + "生产一个产品");
bfull = true;
notifyAll();
} public synchronized void get() {
while (!bfull) {
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println("顾客取走一个产品");
bfull = false;
notifyAll();
}
}
要使用while语句,防止虚假呼唤
楼主你的书不是很好啊