//创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(100);
//创建多个有返回值的任务
List<Future> list = new ArrayList<Future>();
for (int i = 0; i < 100; i++) {
int finalI = i;
Callable callable = () -> {
return finalI;
};
//执行任务并获取 Futrue对象
Future f = pool.submit(callable);
list.add(f);
}
//关闭线程池 停止新任务提交,执行完成之前提交的任务
pool.shutdown();
for (Future future : list) {
System.out.println("i:" + future.get().toString());
}