问题描述
我有一个ExecutorService,用于调用Callable对象的集合,并返回与该集合中Callable元素相对应的Future对象的列表.
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)
我一直在执行的代码是
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
}
它在future.get()
行给了我错误.
请帮助解决障碍或让我知道我还提供什么.
Please help to resolve the roadblock or let me know what else do I provide.
推荐答案
即使Callable中有异常,isDone()
的结果也可能为true.从 isDone
文档:
The result of isDone()
may be true even if there was an exception in the Callable. From the isDone
documentation:
因此,有关"Future"已经完成,但是在计算时遇到了错误.在您发布的堆栈跟踪下,应该有另一个跟踪,列出了"Caused by:"(caused by),这将导致Callable内部出现根故障.
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.
作为示例,请参见此ideone链接的输出. 之后成功检查到isDone
是否为真后,出现了此异常:
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)
这篇关于遍历List< Future>对象抛出IndexOutOfBounds异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!