//一个多生产多消费的多线程通信例子,不知道怎么出问题了?
//生产者-消费者问题俩种方式解决
class Commodity{ //商品类
protected int count=0; //记录仓库商品数量,默认为0
void increase() {count++; //增加一件商品
}
void decrease() {count--; //减少一件商品
}
}
class Produce extends Thread{
private Commodity c;
Produce(Commodity c){this.c=c;}
//当有多个(生产者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while(true) {
while(c.count>=50) { //当数量多于等于50件时,线程休眠,会释放锁
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.increase();
System.out.println("生产线程"+Thread.currentThread().getName()+"生产第"+c.count+"件产品");
this.notifyAll();  //已退出while循环说明仓库未满,恢复第一个休眠的线程
}
}
}
class Consumer extends Thread{
private Commodity c;
Consumer(Commodity c){this.c=c;}
//当有多个(消费者)线程操作,考虑安全性必须加synchronized
public synchronized void run() {
while(true) {
while(c.count<=0) {
try {
this.wait(); //当仓库已空,当前线程休眠,并释放锁。
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.decrease();
System.out.println("消费线程"+Thread.currentThread().getName()+"消费第"+c.count+"件产品");
this.notifyAll();
}
}
}
public static class excise10_11 {
public static void main(String[] args) {
Commodity c=new Commodity();
Produce p1=new Produce(c);
Produce p2=new Produce(c);
Consumer c1=new Consumer(c);
Consumer c2=new Consumer(c);
p1.start();c1.start();c2.start();p2.start();
}
}

解决方案 »

  1.   

    this.notifyAll(); 唤醒消费者线程,生产者依然在等待
      

  2.   


    class Commodity { // 商品类
     int count = 0; // 记录仓库商品数量,默认为0 void increase() {
    count++; // 增加一件商品
    } void decrease() {
    count--; // 减少一件商品
    }
    }class Produce extends Thread {
    private Commodity c; Produce(Commodity c) {
    this.c = c;
    } // 当有多个(生产者)线程操作,考虑安全性必须加synchronized
    public synchronized void run() {
    while (true) {
    while (c.count >= 50) { // 当数量多于等于50件时,线程休眠,会释放锁
    try {
    System.out.println("");
    this.wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    c.increase();
    System.out.println("生产线程" + Thread.currentThread().getName() + "生产第" + c.count + "件产品");
    this.notifyAll(); // 已退出while循环说明仓库未满,恢复第一个休眠的线程

    }
    }
    }class Consumer extends Thread {
    private Commodity c; Consumer(Commodity c) {
    this.c = c;
    } // 当有多个(消费者)线程操作,考虑安全性必须加synchronized
    public synchronized void run() {
    while (true) {
    while (c.count <= 50) {
    try {
    this.wait(); // 当仓库已空,当前线程休眠,并释放锁。
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    c.decrease();
    System.out.println("消费线程" + Thread.currentThread().getName() + "消费第" + c.count + "件产品");
    this.notifyAll();
    }
    }
    }public class excise10_11 {
    public static void main(String[] args) {
    Commodity c = new Commodity();
    Produce p1 = new Produce(c);
    Produce p2 = new Produce(c);
    Consumer c1 = new Consumer(c);
    Consumer c2 = new Consumer(c);
    p1.start();
    c1.start();
    c2.start();
    p2.start();
    }
    }  应该是有个括号放错地方了