【问题标题】:"Thread-1" java.lang.IndexOutOfBoundsException“线程 1”java.lang.IndexOutOfBoundsException
【发布时间】:2013-10-26 19:18:11
【问题描述】:

我的代码是:

 public class Application {
public static void main(String args[]) throws InterruptedException{
    Fifo f = new Fifo();

    Producer pr = new Producer("elso",f);

    pr.go();


    Thread.sleep(2000);
    Consumer cr = new Consumer("csacsi",f,1000);
    cr.start();
}
 } 

 public class Producer extends Thread{

String szoveg;
int szam;
Fifo ff;

Producer(String s, Fifo f){ ff = f; szoveg =s; szam = 0; }

public void go(){
        start();
}

public void run(){
    while(szam<6)
    try {
        sleep(1000);

        ff.put(szoveg+ " "+szam);
        System.out.println("producer" +" " +ff.get()+" "+System.currentTimeMillis()%100000);
        //System.out.println(szoveg +" "+ szam+ "     "+System.currentTimeMillis()%100000);
        szam++;
    } catch (InterruptedException e) {
        System.out.println("fasz");
    }

    //szam+=1;
}
 }

 import java.util.ArrayList;

 public class Fifo {

ArrayList<String> str = new ArrayList<String>();


 synchronized void put(String s) throws InterruptedException{
    while(str.size()>9){

        //Thread.sleep(10); 
    }
    str.add(s);
}

String get() throws InterruptedException{
    synchronized(str){
     if(str.size()==0) str.wait();
     if(str.size()!=0) str.notifyAll();
         //Thread.sleep(10);
     String s = str.get(0);
     str.remove(0);
     return s;}
 }
 }


 public class Consumer extends Thread{
Fifo F;
String S;
int I,asd;

Consumer (String s, Fifo f, int i){
    F=f; S=s; I=i;
}

public void run(){
    while(asd<6){
        try {
            sleep(I);
            System.out.println("consumer "+F.get());
            asd++;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
 }

和输出:

producer elso 0 34491
producer elso 1 35493
producer elso 2 36493Exception in thread "Thread-1" 
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at alap.Fifo.get(Fifo.java:24)
at alap.Consumer.run(Consumer.java:16)
producer elso 3 37495
producer elso 4 38496
producer elso 5 39496

那么,我的错误是什么?

【问题讨论】:

    标签: multithreading exception runnable


    【解决方案1】:

    当您使用synchronized 时,您需要在两个线程中的完全相同的实例上进行同步,这两个线程应该相互保护,否则不会达到预期的效果。

    你的put方法在this上同步,而你的get方法在str上同步,这是两个不同的对象,所以没有必要的保护作用。

    【讨论】:

    • 你能告诉我,如何在两个线程中的完全相同的实例上同步?
    • 你只需要在这两种情况下以相同的方式编写同步。所以把String get() throws InterruptedException改成synchronized String get() throws InterruptedException,把里面的synchronized (str){和匹配的}删掉。
    • 嗯,我认为这就是你所指的。这已经尝试过了。输出:在 alap.Fifo.get(Fifo.java:23) 在 alap.Producer.run(Producer.java) 的 java.lang.Object.notifyAll(Native Method) 的线程“Thread-0”java.lang.IllegalMonitorStateException 中的异常:22) 在 alap.Fifo.get(Fifo.java:23) 在 alap.Consumer.run(Consumer. java:16)
    • 对,所以您需要在完全相同的实例上调用waitnotifyAll,即this
    • 是的,但还是有问题。你可以看到输出。
    猜你喜欢
    • 2013-11-22
    • 2011-07-05
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2018-05-15
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多