Created
March 10, 2017 02:07
-
-
Save qingniufly/c538b4046e1c7e8ed97ca5d984c5acb7 to your computer and use it in GitHub Desktop.
java Executors ExecutorService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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