下面的程序运行的时候会一上来t2消费者线程抛异常Exception in thread "t2" java.lang.ArrayIndexOutOfBoundsException: -1
因为当t2进入等待状态的时候,t4获得所有权,马上t4又进入等待状态,唤醒之前的t2,当t2被唤醒再运行的时候已经是从bag.wait();的下一行运行,所以会抛异常。
请问怎么才能避免呢?我的bag.notify()的位置是不是放的不对呢?
public class PC1 {
public static final int limit=3;
public static void main(String[] args) {
List bag=new ArrayList();
Producer p=new Producer(bag);
Consumer c=new Consumer(bag);
Thread t1=new Thread(p,"t1");Thread t2=new Thread(c,"t2");Thread t3=new Thread(p,"t3");Thread t4=new Thread(c,"t4");
t2.start();t4.start();
t1.start();t3.start();
}
}
class Producer implements Runnable {
private List bag;
public Producer(List bag){
this.bag=bag;
}
@Override
public void run() {
synchronized(bag){
while(true){
if(bag.size()>=PC1.limit){
bag.notify();
try {bag.wait();} catch (InterruptedException e) {e.printStackTrace();}
}
int curr=bag.size();
bag.add(++curr);
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+" produce "+curr);
}
}
}
}
class Consumer implements Runnable {
private List bag;
public Consumer(List bag){
this.bag=bag;
}
@Override
public void run() {
synchronized(bag){
while(true){
if(bag.size()<=0){
bag.notify();
try {bag.wait();} catch (InterruptedException e) {e.printStackTrace();}
}
int curr=bag.size();
bag.remove(curr-1);
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+" consume "+bag.size());
}
}
}
}