Reputation: 38910
My code is working with submit() of callable to ExecutorService. But when I tried to change it to invokeAll(), I am getting compilation error.
code:
public class FutureDemo{
public FutureDemo(){
ExecutorService service = Executors.newFixedThreadPool(10);
/*
for ( int i=0; i<10; i++){
MyCallable myCallable = new MyCallable((long)i);
Future<Long> futureResult = service.submit(myCallable);
Long result = null;
try{
result = futureResult.get(5000, TimeUnit.MILLISECONDS);
}catch(TimeoutException e){
System.out.println("Time out after 5 seconds");
futureResult.cancel(true);
}catch(InterruptedException ie){
System.out.println("Error: Interrupted");
}catch(ExecutionException ee){
System.out.println("Error: Execution interrupted");
}
System.out.println("Result:"+result);
}
*/
List<MyCallable> futureList = new ArrayList<MyCallable>();
for ( int i=0; i<10; i++){
MyCallable myCallable = new MyCallable((long)i);
futureList.add(myCallable);
}
try{
Future<Long> futures = service.invokeAll(futureList); // error
}catch(Exception err){
err.printStackTrace();
}
service.shutdown();
}
public static void main(String args[]){
FutureDemo fc = new FutureDemo();
}
class MyCallable implements Callable{
Long id = 0L;
public MyCallable(Long val){
this.id = val;
}
public Long call(){
return id;
}
}
}
Error:
FutureDemo.java:31: error: no suitable method found for invokeAll(List<FutureDemo.MyCallable>)
Future<Long> futures = service.invokeAll(futureList); // error
^
method ExecutorService.<T#1>invokeAll(Collection<? extends
Callable<T#1>>,long,TimeUnit) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
method ExecutorService.<T#2>invokeAll(Collection<? extends Callable<T#2>>) is not applicable
(no instance(s) of type variable(s) T#2 exist so that argument type List<FutureDemo.MyCallable> conforms to formal parameter type Collection<? extends Callable<T#2>>)
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>invokeAll(Collection<? extends Callable<T#1>>,long,TimeUnit)
T#2 extends Object declared in method <T#2>invokeAll(Collection<? extends Callable<T#2>>)
Upvotes: -1
Views: 1492
Reputation: 279920
For one, invokeAll
returns a List<Future<T>>
, but you're trying to assign the result to a variable of type Future<T>
.
Two, you're passing a List<MyCallable>
as an argument, where a MyCallable
is a raw Callable
. Being raw, generics are erased and the compiler cannot infer an appropriate type that would match the expected Collection<? extends Callable<T>>
parameter type.
Don't use raw types. Read
Then appropriately change your class declaration to
class MyCallable implements Callable<Long> {
Upvotes: 3