Search code examples
javamultithreadingindexoutofboundsexceptionjava.util.concurrent

java.lang.ArrayIndexOutOfBoundsException: -1 future.get() multithreading


Why does my app throw a

java.lang.ArrayIndexOutOfBoundsException: -1

when I invoke future.get() on java.utils.concurrent.Future??

   List<Future> tableLoadings = new LinkedList<>();
   ExecutorService executor = Executors.newFixedThreadPool(8);
    try{
        for(Entry<Integer, String> entry: farmIds.entrySet())
        {   
            int id = entry.getKey();
            String username = entry.getValue(); 
            
            psLog.println("START ELABORAZIONE FARMACIA ID : "+ id+" TPH_USERNAME : "+username );    
            /*SdajSdaj*/
            tableLoadings.add(executor.submit(new StatusMultiThreading(id, username, psLog, connSTORY, connCF, mongoDatabase)));
        }
        for (Future<Void> future : tableLoadings) {         
                try{
                    future.get();           
                }catch(Exception e){
                    psLog.println("[EE] ERORE ELABORAZIONE THREAD FARMACIA [EE] "+e.getMessage());
                }
        }
    }finally{
        executor.shutdown(); 
        psLog.println("END CONSOLIDA STATUS FARMACIE");
    }           

this is the log..

START ELABORAZIONE FARMACIA ID : 62 TPH_USERNAME : A0102987
START ELABORAZIONE FARMACIA ID : 63 TPH_USERNAME : A0103019
START SENDING DATA TO DB FARMID = 66
...
START SENDING DATA TO DB FARMID = 17
[EE] ERORE ELABORAZIONE THREAD FARMACIA [EE] java.lang.ArrayIndexOutOfBoundsException: -1
[EE] ERRORE ELABORAZIONE THREAD FARMACIA [EE] java.lang.ArrayIndexOutOfBoundsException: -1
END CONSOLIDA STATUS FARMACIE

I can't find anything wrong if I debug.

I cannot go inside .get() method, so I don't understand which line of code is invalid.


Solution

  • What can be said so far: you use the ExecutorService to pass in tasks:

    new StatusMultiThreading(id, username, psLog, connSTORY, connCF, mongoDatabase)
    

    Later on, when you call get() the corresponding task is triggered. So that exception takes place inside that class of yours.