4

I have an ExecutorService which is used to invoke a Collection of Callable obejcts and returns a List of Future objects corresponding to Callable elements in the collection.

However, somewhere while traversing the list, it throws the following exception :

java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 7, Size: 1
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.soc.transformcsv.ParallelTransformationPool.doExecute(ParallelTransformationPool.java:91)

The code I have been executing is

List<Future<StringBuilder>> futures = executor.invokeAll(builders);
executor.shutdown();
HashMap<String, List<StringBuilder>> allServiceTypeRows = new LinkedHashMap<>();

for (Future<StringBuilder> future : futures) {
    // I have tried putting future.isDone() which always prints true before the exception
    StringBuilder recordBuilder = future.get();
    // do more
}

It gives me error at future.get() line.

Please help to resolve the roadblock or let me know what else do I provide.

Kanav Sharma
  • 307
  • 1
  • 5
  • 13

2 Answers2

4

The result of isDone() may be true even if there was an exception in the Callable. From the isDone documentation:

Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

So the Future in question is done, but encountered an error while it was computing. Under the stack trace you posted, there should be another one, that lists "Caused by:"—that will have the root failure from within your Callable.

As an example, see the output from this ideone link. This exception comes after successfully checking that isDone is true:

Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.RuntimeException: This is a failure!
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at Ideone.main(Main.java:16)
Caused by: java.lang.RuntimeException: This is a failure!
    at Ideone$SampleCallable.call(Main.java:21)
    at Ideone$SampleCallable.call(Main.java:19)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at Ideone.main(Main.java:14)
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • yes, the root failure provides me `OutOfMemoryError` and I get that it occurs while the execution of a particular task. I am on it. – Kanav Sharma Jul 24 '15 at 05:33
2

The exception is not occurring by traversal of List<Future>. Actually IndexOutOfBoundsException occurred while ExecutorService was executing call() method of your Callable(one of the builders).

But this exception is thrown (wrapped inside ExecutionException) only when you try to get the result of Future using future.get().

Mrinal
  • 1,846
  • 14
  • 17