方法说明:
/**
* Executes the given tasks, returning a list of Futures holding
* their status and results when all complete.
* {@link Future#isDone} is {@code true} for each
* element of the returned list.
* Note that a <em>completed</em> task could have
* terminated either normally or by throwing an exception.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param <T> the type of the values returned from the tasks
* @return a list of Futures representing the tasks, in the same
* sequential order as produced by the iterator for the
* given task list, each of which has completed
* @throws InterruptedException if interrupted while waiting, in
* which case unfinished tasks are cancelled
* @throws NullPointerException if tasks or any of its elements are {@code null}
* @throws RejectedExecutionException if any task cannot be
* scheduled for execution
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
运行结果可能出现的情况:
- 全部运行成功
- 部分运行失败,剩余任务被取消
玩具代码:
import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; //运行多个任务并处理所有结果 public class Result { private String name; private int value; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public static class Task implements Callable<Result>{ private String name; public Task(String name){ this.name = name; } @Override public Result call() throws Exception { System.out.printf("%s : Staring \n", this.name); try{ long duration = (long)(Math.random()*10); System.out.printf("%s:Waiting %d seconds for results . \n", this.name,duration); TimeUnit.SECONDS.sleep(duration); }catch(InterruptedException e){ e.printStackTrace(); } int value = 0; for(int i=0;i<5;i++){ value += (int)(Math.random()*100); } Result result = new Result(); result.setName(name); result.setValue(value); System.out.println(this.name+": Ends"); return result; } } public static void main(String[] args) { ExecutorService executor =(ExecutorService)Executors.newCachedThreadPool(); List<Task> taskList = new ArrayList<>(); for(int i=0;i<3;i++){ Task task = new Task(Integer.toString(i)); taskList.add(task); } List<Future<Result>> resultList = new ArrayList<>(); try { //等待所有任务执行完成 executor.invokeAll(taskList); } catch (InterruptedException e) { e.printStackTrace(); } executor.shutdown(); System.out.printf("Main: Printing the results \n"); for(int i=0;i<resultList.size();i++){ Future<Result> future = resultList.get(i); try{ Result result = future.get(); System.out.printf("%s : %s \n",result.getName(),result.getValue()); }catch(InterruptedException | ExecutionException e){ e.printStackTrace(); } } } }
AbstractExecutorService.invokeAll实现逻辑:
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException { if (tasks == null) throw new NullPointerException(); ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); boolean done = false; try { for (Callable<T> t : tasks) { RunnableFuture<T> f = newTaskFor(t); futures.add(f); execute(f); } for (int i = 0, size = futures.size(); i < size; i++) { Future<T> f = futures.get(i); if (!f.isDone()) { try { f.get(); } catch (CancellationException ignore) { } catch (ExecutionException ignore) { } } } done = true; return futures; } finally { if (!done) for (int i = 0, size = futures.size(); i < size; i++) futures.get(i).cancel(true); } }