執行給定的任務,返回完成所有狀態和結果的Futures列表。Future.isDone()對於返回列表的每個元素都是true 。
注意,完成的任務可以正常終止,也可以引發異常,此方法的結果是未定義的,如果在執行這個操作時修改了給定的Collection,則。
示例代碼Fragment:import java.util.concurrent.*;
import java.util.*;
public class InvokeAllDemo{
public InvokeAllDemo(){
System.out.println(" creating service" );
ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
List futureList = new ArrayList();
for ( int i=0; i<10; i++){
MyCallable myCallable = new MyCallable((long)i);
futureList.add(myCallable);
}
System.out.println(" Start" );
try{
List> futures = service.invokeAll(futureList);
}catch(Exception err){
err.printStackTrace();
}
System.out.println(" Completed" );
service.shutdown();
}
public static void main(String args[]){
InvokeAllDemo demo = new InvokeAllDemo();
}
class MyCallable implements Callable{
Long id = 0L;
public MyCallable(Long val){
this.id = val;
}
public Long call(){
//Add your business logic
return id;
}
}
}