ExecutorService 在提交时工作正常,但在 invokeAll 时抛出错误
ExecutorService working fine with submit but throwing error for invokeAll
我的代码正在使用可调用 ExecutorService 的 submit()。但是当我试图将它更改为 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>>)
例如,invokeAll
returns a List<Future<T>>
,但您试图将结果分配给 Future<T>
.
类型的变量
第二,您将 List<MyCallable>
作为参数传递,其中 MyCallable
是原始 Callable
。作为原始的,泛型被删除,编译器无法推断出与预期的 Collection<? extends Callable<T>>
参数类型相匹配的适当类型。
不要使用原始类型。阅读
- What is a raw type and why shouldn't we use it?
然后将您的 class 声明适当更改为
class MyCallable implements Callable<Long> {
我的代码正在使用可调用 ExecutorService 的 submit()。但是当我试图将它更改为 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>>)
例如,invokeAll
returns a List<Future<T>>
,但您试图将结果分配给 Future<T>
.
第二,您将 List<MyCallable>
作为参数传递,其中 MyCallable
是原始 Callable
。作为原始的,泛型被删除,编译器无法推断出与预期的 Collection<? extends Callable<T>>
参数类型相匹配的适当类型。
不要使用原始类型。阅读
- What is a raw type and why shouldn't we use it?
然后将您的 class 声明适当更改为
class MyCallable implements Callable<Long> {