有谁能帮我看下这段代码的问题,帮忙解决一下:class ProducerConsumer {
public static void main(String args[]) {
BasketStack bsk = new BasketStack();
Producer p1 = new Producer(bsk);
Consumer c1 = new Consumer(bsk);
Thread t1 = new Thread(p1);
Thread t2 = new Thread(c1);
t1.start();
t2.start();
}
}class Woto {
  int id;
Woto(int id) {
this.id = id;
}

public String toString() {
return "WoTou : " + id;
}
}class BasketStack {
private int index = 0;
Woto amount[] = new Woto[10];

public void push(Woto id) {
index++;
amount[index] = id;
}

 public Woto pop() {
index--;
return amount[index];
}
}class Producer implements Runnable {
BasketStack bs = null;

Producer(BasketStack bs) {
this.bs = bs;
}

public void run() {
for(int i = 0;i<10;i++) {
bs.push(new Woto(i));
System.out.println("生产了:"+i);
}
}
}class Consumer implements Runnable {
BasketStack bs = null;

Consumer(BasketStack bs) {
this.bs = bs;
}

public void run() {
for(int i = 0;i<10;i++) {
Woto wt = bs.pop();
System.out.println("消费了:"+wt);
}
}
}

解决方案 »

  1.   

    问题是什么?
    我运行了一下没什么大问题,有的问题是和线程无关的……非线程方面:
    运行时报错是 java.lang.ArrayIndexOutOfBoundsException,这说明是你代码中的数组越界造成的,这儿有个小错误,在class BasketStack中的push方法中,先对index进行了自加,然后才进行的数组操作,于是一开始的时候,第一个id数据是存在amount[1]里的,而不是预想中的amount[0],于是当存第10个id的时候,本以为index应该是9,但其实这个时候已经是10了,而amount最大的位置是9,所以越界报错了。
    可以把index++这句放到amount[index]=id;后面,这样就可以解决这个问题。:)
    线程方面:
    执行的时候总是能看见先是全部生产了,然后全部消费。好像看不出什么线程执行的先后顺序问题。我建议在Producer的for循环里加上
    try {
        Thread.sleep(5);
        } catch (InterruptedException e) {
        e.printStackTrace();
    }
    在Consumer的for循环里加上
    try{
        Thread.sleep(10);
        }catch(InterruptedException e){
        e.printStackTrace();
    }
    其中5和10是随意捏造的数字,只要保证Producer的执行速度比Consumer的快就行了(否则还会出现数组越界错误,这个时候是pop()出错,而不是push()),这样就可以看出两个交替进行的情况了:)另外废话一句,不知道你知不知道,呵呵。
    java.lang.ArrayIndexOutOfBoundsException后面跟的那个数字是发生越界错误时将要进行存取的数组序号,从此可以判断一下是什么地方出了问题~,以后可以用这个来自己看看
      

  2.   

    你的代码问题还比较,数组越界没处理,同步没处理,给你改了一下,不过感觉还不是很好,给你参考而已public class ProducerConsumer {
    public static void main(String args[]) {
    BasketStack bsk = new BasketStack();
    Producer p1 = new Producer(bsk);
    Consumer c1 = new Consumer(bsk);
    Thread t1 = new Thread(p1);
    Thread t2 = new Thread(c1);
    t1.start();
    t2.start();
    }
    }class Woto {
    int id; Woto(int id) {
    this.id = id;
    } public String toString() {
    return "WoTou : " + id;
    }
    }class BasketStack {
    private int index = 0;
    Woto amount[] = new Woto[10]; public synchronized void push(Woto id) {
    while (index >= amount.length) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    amount[index++] = id;
    notifyAll();
    } public synchronized Woto pop() {
    index--;
    while (index < 0) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    return amount[index];
    }
    }class Producer implements Runnable {
    BasketStack bs = null; Producer(BasketStack bs) {
    this.bs = bs;
    } public void run() {
    for (int i = 0; i < 10; i++) {
    bs.push(new Woto(i));
    System.out.println("生产了:" + i);
    }
    }
    }class Consumer implements Runnable {
    BasketStack bs = null; Consumer(BasketStack bs) {
    this.bs = bs;
    } public void run() {
    Woto wt = null;
    for(int i = 0; i < bs.amount.length; i++) {
    wt = bs.pop();
    System.out.println("消费了:" + wt);
    }
    }
    }
      

  3.   

      非常感谢你的解答.我怎么编译时能够通过.在运行时就提示下面的错误.是不是没有找到类而出现的错误.
    Exception in thread "main" java.lang.NoClassDefFoundError: ProducerConsumer
      

  4.   

    在我这里没什么问题了,那估计是你的环境还有问题、或者你打包了,但运行的时候命令没写好
    程序基本没什么问题了
    你把代码拷到IDE(我用eclipse)上跑看看
      

  5.   

      昨晚参照你的代码,研究了一下.还是把问题解决了.非常感谢.你有什么联系方式吗? 以后有什么问题我真的多向你请教.我还是个java初学者.