这个程序是教材上的 刚刚开始学习生产者消费者问题 
看着程序也没什么问题 为什么一运行就不但没有结果 而且无限循环
只有一个“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();
}
}
}

解决方案 »

  1.   

      while (true) 肯定是死循环
      

  2.   

    出现死锁的可能原因:Producer执行put,并且把put函数完整地执行了一遍,包括notify也执行完了之后Consumer执行get,因为bfull为true,所以一直进不了if语句里面在然后Producer执行put,因为bfull为true,所以进入wait状态接下来就只有Consumer一直在执行get了,但bfull为true,它一直进不了if语句,无法执行notify语句,最终导致死锁为了避免这种情况,你应该修改一下get函数,改成这样:public synchronized void get() {
            if (!bfull) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            System.out.println("顾客取走一个产品");
            bfull = false;
            notify();
        }
      

  3.   

    LZ的Operation 有问题,改一下:class Operation {
        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();
        }
    }
      

  4.   

    多线程的代码,由于执行顺序的不确定性,导致出了问题很难去debug
    只能通过知识和经验去人工分析代码如楼上所说,get方法写错了
    因为如果get调用前,已经 bfull为true的话,get就直接返回了,啥也没拿到
    而put的,因为阻塞,也只能生产一个出来
      

  5.   

    对于多线程并发,推荐使用 notifyAll()
    并且在被唤醒后,再次检查条件是否满足。当然,一般情况下要用到循环等待
    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();
    }
    }
      

  6.   

    五楼的哥们的回答才是最好的;
    要使用while语句,防止虚假呼唤
    楼主你的书不是很好啊