public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
List<Callable<Map>> tasks = Lists.newArrayList();
tasks.add(new Callable<Map>() {
@Override
public Map call() throws Exception {
Map map = new HashMap();
map.put("1",1);
return map;
}
});
tasks.add(new Callable<Map>() {
@Override
public Map call() throws Exception {
Map map = new HashMap();
map.put("2",2);
return map;
}
});
tasks.add(new Callable<Map>() {
@Override
public Map call() throws Exception {
Map map = new HashMap();
map.put("3",3);
return map;
}
});
List<Map> list = new ArrayList();
try {
List<Future<Map>> futureList = executorService.invokeAll(tasks);
for (Future<Map> future : futureList) {
try {
Map map = future.get();
System.out.println(map);
list.add(map);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.shutdown();
list.forEach(o ->{
System.out.println(o);
});
}
使方法并行执行DEMO
最新推荐文章于 2024-07-10 21:37:49 发布