Skip to content

Instantly share code, notes, and snippets.

@qingniufly
Created March 10, 2017 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save qingniufly/c538b4046e1c7e8ed97ca5d984c5acb7 to your computer and use it in GitHub Desktop.
Save qingniufly/c538b4046e1c7e8ed97ca5d984c5acb7 to your computer and use it in GitHub Desktop.
java Executors ExecutorService
ExecutorService es = Executors.newCachedThreadPool();
ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<>(es);
Set<Future> futureSet = new HashSet<Future>();
List<Callable> callerList = new ArrayList<Callable>();
// 构造任务列表
for (int i = 0; i < 100; i++) {
callerList.add(...);
}
// 提交任务,并添加到等待结果集合
for (Callable caller : callerList) {
Future<Boolean> f = ecs.submit(caller);
futureSet.add(f);
}
while (!futureSet.isEmpty()) {
Future<Boolean> f = ecs.take();
// 取得结果
System.out.println(f.get());
// 从等待结果集合中移除
futureSet.remove(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment