在多线程中使用ArrayList为啥出现java.lang.IndexOutOfBoundsException:错误?
importjava.awt.List;importjava.util.ArrayList;importjava.util.LinkedList;publicclassT...
import java.awt.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class Thread13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cangku cangku=new Cangku();
Provider p=new Provider(cangku);
p.setNum(30);
Coustomer c=new Coustomer(cangku);
c.setNum(20);
Thread t=new Thread(p);
Thread a=new Thread(c);
t.start();
a.start();
}
}
class Cangku {
private final int MAX_SIZE=100;
private final int MIN_SIZE=0;
ArrayList<Object> list=new ArrayList<Object>();
public void provider(int num) {
synchronized(this) {
while(list.size()+num>MAX_SIZE) {
System.out.println("要生产的数量:"+num+"------仓库存量:"+list.size()+"-----现在不能生产");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i=0; i<num; i++) {
list.add(new Object());
}
System.out.println("已经生产数量:"+num+"------仓库存量"+list.size());
notifyAll();
}
}
public void coustomer(int num) {
synchronized(this) {
while(list.size()<num) {
System.out.println("要消费的数量:"+num+"------仓库存量:"+list.size()+"-----现在不能消费");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("已经消费数量:"+num+" "+list.size());
for(int i=0; i<num; i++) {
list.remove(i);
}
System.out.println("已经消费数量:"+num+"------仓库存量"+list.size());
notifyAll();
}
}
}
class Provider implements Runnable {
Cangku cangku;
private int num;
Provider(Cangku cangku) {
this.cangku=cangku;
}
@Override
public void run() {
// TODO Auto-generated method stub
cangku.provider(num);
}
public void setNum(int num) {
this.num = num;
}
}
class Coustomer implements Runnable {
Cangku cangku;
private int num;
Coustomer(Cangku cangku) {
this.cangku=cangku;
}
@Override
public void run() {
// TODO Auto-generated method stub
cangku.coustomer(num);
}
public void setNum(int num) {
this.num = num;
}
} 展开
import java.util.ArrayList;
import java.util.LinkedList;
public class Thread13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cangku cangku=new Cangku();
Provider p=new Provider(cangku);
p.setNum(30);
Coustomer c=new Coustomer(cangku);
c.setNum(20);
Thread t=new Thread(p);
Thread a=new Thread(c);
t.start();
a.start();
}
}
class Cangku {
private final int MAX_SIZE=100;
private final int MIN_SIZE=0;
ArrayList<Object> list=new ArrayList<Object>();
public void provider(int num) {
synchronized(this) {
while(list.size()+num>MAX_SIZE) {
System.out.println("要生产的数量:"+num+"------仓库存量:"+list.size()+"-----现在不能生产");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i=0; i<num; i++) {
list.add(new Object());
}
System.out.println("已经生产数量:"+num+"------仓库存量"+list.size());
notifyAll();
}
}
public void coustomer(int num) {
synchronized(this) {
while(list.size()<num) {
System.out.println("要消费的数量:"+num+"------仓库存量:"+list.size()+"-----现在不能消费");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("已经消费数量:"+num+" "+list.size());
for(int i=0; i<num; i++) {
list.remove(i);
}
System.out.println("已经消费数量:"+num+"------仓库存量"+list.size());
notifyAll();
}
}
}
class Provider implements Runnable {
Cangku cangku;
private int num;
Provider(Cangku cangku) {
this.cangku=cangku;
}
@Override
public void run() {
// TODO Auto-generated method stub
cangku.provider(num);
}
public void setNum(int num) {
this.num = num;
}
}
class Coustomer implements Runnable {
Cangku cangku;
private int num;
Coustomer(Cangku cangku) {
this.cangku=cangku;
}
@Override
public void run() {
// TODO Auto-generated method stub
cangku.coustomer(num);
}
public void setNum(int num) {
this.num = num;
}
} 展开
2个回答
2016-12-28
展开全部
只是因为 超出范围了
再看看报错的行,然后解决问题
~~~~~~~~~
集合中的数量一直是在变化的,却使用一个固定的值去移除
再看看报错的行,然后解决问题
~~~~~~~~~
集合中的数量一直是在变化的,却使用一个固定的值去移除
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |