ExecutorService正常工作提交但抛出invokeAll错误

时间:2015-10-27 19:49:46

标签: java multithreading executorservice java.util.concurrent

我的代码正在使用可调用的submit()到ExecutorService。但是当我尝试将其更改为invokeAll()时,我收到了编译错误。

代码:

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;
        }
    }
}

错误:

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>>)

1 个答案:

答案 0 :(得分:2)

首先,invokeAll会返回List<Future<T>>,但您尝试将结果分配给Future<T>类型的变量。

其中两个,您传递List<MyCallable>作为参数,其中MyCallable是原始Callable。作为原始,泛型被擦除,编译器无法推断出与预期Collection<? extends Callable<T>>参数类型匹配的适当类型。

不要使用原始类型。阅读

然后适当地将您的班级声明更改为

class MyCallable implements Callable<Long> {