1,完成多任务的线程自动负载均衡
2,完成多线程结果归并
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<Integer>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Future<Integer> future = executorService.submit(new Callable<Integer>() {
public Integer call() throws Exception {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("111");
return sum;
}
});
list.add(future);
}
for (Future<Integer> integerFuture : list) {
Integer s = integerFuture.get();
System.out.println(s);
}
executorService.shutdown();
输出10次 5050,不执行integerFuture.get();也会打印 111,表明call是被执行的。