有谁能帮我看下这段代码的问题,帮忙解决一下: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);
}
}
}
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);
}
}
}
我运行了一下没什么大问题,有的问题是和线程无关的……非线程方面:
运行时报错是 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后面跟的那个数字是发生越界错误时将要进行存取的数组序号,从此可以判断一下是什么地方出了问题~,以后可以用这个来自己看看
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);
}
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: ProducerConsumer
程序基本没什么问题了
你把代码拷到IDE(我用eclipse)上跑看看