I need to launch a bunch of tasks in concurrent threads and retrieve its results.
Here is my code:
List<Callable<? extends Object>> tasks = new ArrayList<>();
// Adding some tasks whith return different types of results:
// Callable<Double>, Callable<String>, Callable<SomeOtherType>, and so on...
List<Future<? extends Object>> results = executor.invokeAll( tasks );
But the IDE shows me the next error:
no suitable method found for invokeAll(List<Callable<? extends Object>>)
method ExecutorService.<T#1>invokeAll(Collection<? extends Callable<T#1>>)
is not applicable
(cannot infer type-variable(s) T#1
(argument mismatch; List<Callable<? extends Object>> cannot be converted
to Collection<? extends Callable<T#1>>
method ExecutorService.<T#2>invokeAll(Collection<? extends Callable<T#2>>,long,TimeUnit)
is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
where T#1,T#2 are type-variables:
T#1 extends Object declared in method
<T#1>invokeAll(Collection<? extends Callable<T#1>>)
T#2 extends Object declared in method
<T#2>invokeAll(Collection<? extends Callable<T#2>>,long,TimeUnit)
The method signature is:
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
Obviously, I can replace <? extends Object>
with <Object>
, and make all of my tasks return Object
(e.g. replace SomeTask1 implements Callable<Double>
with SomeTask1 implements Callable<Object>
).
But my question is: why this error occurs? I do not understand why I can't code like this. Can anyone make it clear?