我写了一个生产者与消费者程序 当生产者生产满时线程等待 消费者消费完线程等待 两个线程却不能被唤醒,高人求解!package com.thread;import java.util.*;//主类
public class MakerConsumer{
public static void main(String[] args) {
Maker maker = new Maker();
Consumer consumer = new Consumer();
Thread tmaker = new Thread(maker);
Thread cmaker = new Thread(consumer);
tmaker.start();
cmaker.start();
}
}//馒头
class Mantou{
int id;
public Mantou(int id){
this.id=id;
}
}//篓子
class Basket{
static int num=0;
static List<Mantou> basket = new ArrayList<Mantou>();
public synchronized void push(Mantou mantou){
while(num>5){
System.out.println("=========篓子满了,停止生产!!!");
try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}
}
notifyAll();
basket.add(mantou);
num++;
}

public synchronized Mantou pop(){
Mantou mantou=null;
while(num==0){
System.out.println("*********篓子空了,停止消费!!!");
try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}
}
notifyAll();
if(num>0){
mantou = basket.get(num-1);
basket.remove(num-1);
num--;
return mantou;
}else{
System.out.println("篓子空了... ...");
return null;
}
}

}//生产者
class Maker implements Runnable{
boolean makerflag = true;
static int index = 0;
public void run() {
while(makerflag){
ThreadHelper.sleep((int)(Math.random()*2000));
index++;
new Basket().push(new Mantou(index));
System.out.println("@生产者@生产了第【"+index+"】个馒头... ...");
}
}
}//消费者
class Consumer implements Runnable{
boolean consumerflag=true;
Mantou mantou;
public void run() {
while(consumerflag){
ThreadHelper.sleep((int)(Math.random()*2000));
mantou=new Basket().pop();
if(mantou!=null){
System.out.println("【消费者】消费了第【"+mantou.id+"】个馒头... ...");
}
}
}
}//线程帮助
class ThreadHelper{
public static void sleep(long time){
try {Thread.sleep(time);} catch (InterruptedException e) {e.printStackTrace();}
}
}

解决方案 »

  1.   


    import java.util.*;//主类
    public class MakerConsumer {
    public static void main(String[] args) {
    //因为生产者和消费者共用一个篓子,所以在这里生成一个篓子,
    //然后给生产者和消费者都把这个篓子传进去,这样的话,就要在生产者和消费者类里各加一个Basket的
    //成员变量。在run里面用传进去的这个篓子,这样他们就公用一个篓子。
    Basket basket = new Basket();
    Maker maker = new Maker(basket);
    Consumer consumer = new Consumer(basket);
    Thread tmaker = new Thread(maker);
    Thread cmaker = new Thread(consumer);
    tmaker.start();
    cmaker.start();
    }
    }// 馒头
    class Mantou {
    int id; public Mantou(int id) {
    this.id = id;
    }
    }// 篓子
    class Basket {
    static int num = 0;
    static List<Mantou> basket = new ArrayList<Mantou>(); public synchronized void push(Mantou mantou) {
    while (num > 5) {
    System.out.println("=========篓子满了,停止生产!!!");
    try {
    this.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    basket.add(mantou);
    num++;
    } public synchronized Mantou pop() {
    Mantou mantou = null;
    while (num == 0) {
    System.out.println("*********篓子空了,停止消费!!!");
    try {
    this.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    if (num > 0) {
    mantou = basket.get(num - 1);
    basket.remove(num - 1);
    num--;
    return mantou;
    } else {
    System.out.println("篓子空了... ...");
    return null;
    }
    }}// 生产者
    class Maker implements Runnable {
    boolean makerflag = true;
    static int index = 0;

    private Basket basket;

    public Maker(Basket basket) {
    this.basket = basket;
    } public void run() {
    while (makerflag) {
    ThreadHelper.sleep((int) (Math.random() * 2000));
    index++;
    basket.push(new Mantou(index));
    System.out.println("@生产者@生产了第【" + index + "】个馒头... ...");
    }
    }
    }// 消费者
    class Consumer implements Runnable {
    boolean consumerflag = true;
    Mantou mantou;

    private Basket basket;

    public Consumer(Basket basket) {
    this.basket = basket;
    }

    public void run() {
    while (consumerflag) {
    ThreadHelper.sleep((int) (Math.random() * 2000));
    mantou = basket.pop();
    if (mantou != null) {
    System.out.println("【消费者】消费了第【" + mantou.id + "】个馒头... ...");
    }
    }
    }
    }// 线程帮助
    class ThreadHelper {
    public static void sleep(long time) {
    try {
    Thread.sleep(time);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }代码的修改说明在程序中做了注释,楼主可以看看!
      

  2.   


    import java.util.*;//主类
    public class MakerConsumer {
    public static void main(String[] args) {
    //因为生产者和消费者共用一个篓子,所以在这里生成一个篓子,
    //然后给生产者和消费者都把这个篓子传进去,这样的话,就要在生产者和消费者类里各加一个Basket的
    //成员变量。在run里面用传进去的这个篓子,这样他们就公用一个篓子。
    Basket basket = new Basket();
    Maker maker = new Maker(basket);
    Consumer consumer = new Consumer(basket);
    Thread tmaker = new Thread(maker);
    Thread cmaker = new Thread(consumer);
    tmaker.start();
    cmaker.start();
    }
    }// 馒头
    class Mantou {
    int id; public Mantou(int id) {
    this.id = id;
    }
    }// 篓子
    class Basket {
    static int num = 0;
    static List<Mantou> basket = new ArrayList<Mantou>(); public synchronized void push(Mantou mantou) {
    while (num > 5) {
    System.out.println("=========篓子满了,停止生产!!!");
    try {
    this.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    basket.add(mantou);
    num++;
    } public synchronized Mantou pop() {
    Mantou mantou = null;
    while (num == 0) {
    System.out.println("*********篓子空了,停止消费!!!");
    try {
    this.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    if (num > 0) {
    mantou = basket.get(num - 1);
    basket.remove(num - 1);
    num--;
    return mantou;
    } else {
    System.out.println("篓子空了... ...");
    return null;
    }
    }}// 生产者
    class Maker implements Runnable {
    boolean makerflag = true;
    static int index = 0;

    private Basket basket;

    public Maker(Basket basket) {
    this.basket = basket;
    } public void run() {
    while (makerflag) {
    ThreadHelper.sleep((int) (Math.random() * 2000));
    index++;
    basket.push(new Mantou(index));
    System.out.println("@生产者@生产了第【" + index + "】个馒头... ...");
    }
    }
    }// 消费者
    class Consumer implements Runnable {
    boolean consumerflag = true;
    Mantou mantou;

    private Basket basket;

    public Consumer(Basket basket) {
    this.basket = basket;
    }

    public void run() {
    while (consumerflag) {
    ThreadHelper.sleep((int) (Math.random() * 2000));
    mantou = basket.pop();
    if (mantou != null) {
    System.out.println("【消费者】消费了第【" + mantou.id + "】个馒头... ...");
    }
    }
    }
    }// 线程帮助
    class ThreadHelper {
    public static void sleep(long time) {
    try {
    Thread.sleep(time);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }代码的修改说明在程序中做了注释,楼主可以看看!
      

  3.   


    import java.util.*;//主类
    public class MakerConsumer {
    public static void main(String[] args) {
    //因为生产者和消费者共用一个篓子,所以在这里生成一个篓子,
    //然后给生产者和消费者都把这个篓子传进去,这样的话,就要在生产者和消费者类里各加一个Basket的
    //成员变量。在run里面用传进去的这个篓子,这样他们就公用一个篓子。
    Basket basket = new Basket();
    Maker maker = new Maker(basket);
    Consumer consumer = new Consumer(basket);
    Thread tmaker = new Thread(maker);
    Thread cmaker = new Thread(consumer);
    tmaker.start();
    cmaker.start();
    }
    }// 馒头
    class Mantou {
    int id; public Mantou(int id) {
    this.id = id;
    }
    }// 篓子
    class Basket {
    static int num = 0;
    static List<Mantou> basket = new ArrayList<Mantou>(); public synchronized void push(Mantou mantou) {
    while (num > 5) {
    System.out.println("=========篓子满了,停止生产!!!");
    try {
    this.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    basket.add(mantou);
    num++;
    } public synchronized Mantou pop() {
    Mantou mantou = null;
    while (num == 0) {
    System.out.println("*********篓子空了,停止消费!!!");
    try {
    this.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    if (num > 0) {
    mantou = basket.get(num - 1);
    basket.remove(num - 1);
    num--;
    return mantou;
    } else {
    System.out.println("篓子空了... ...");
    return null;
    }
    }}// 生产者
    class Maker implements Runnable {
    boolean makerflag = true;
    static int index = 0;

    private Basket basket;

    public Maker(Basket basket) {
    this.basket = basket;
    } public void run() {
    while (makerflag) {
    ThreadHelper.sleep((int) (Math.random() * 2000));
    index++;
    basket.push(new Mantou(index));
    System.out.println("@生产者@生产了第【" + index + "】个馒头... ...");
    }
    }
    }// 消费者
    class Consumer implements Runnable {
    boolean consumerflag = true;
    Mantou mantou;

    private Basket basket;

    public Consumer(Basket basket) {
    this.basket = basket;
    }

    public void run() {
    while (consumerflag) {
    ThreadHelper.sleep((int) (Math.random() * 2000));
    mantou = basket.pop();
    if (mantou != null) {
    System.out.println("【消费者】消费了第【" + mantou.id + "】个馒头... ...");
    }
    }
    }
    }// 线程帮助
    class ThreadHelper {
    public static void sleep(long time) {
    try {
    Thread.sleep(time);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }代码的修改说明在程序中做了注释,楼主可以看看!