이 예제에서는 몇 가지 파일을 내 comparator 개체에 제출합니다. 파일이 제출되는 순서가 반환되는 순서와 항상 같지는 않다는 것을 제외하고는 모두 잘 작동합니다. 내가 더 잘 제어 할 수있는 방법에 대한 제안?Java Futures가 "제출"되는 주문을 제어하는 방법은 무엇입니까?
ExecutorService pool = Executors.newFixedThreadPool(5);
CompletionService<Properties> completion = new ExecutorCompletionService<Properties>(pool);
for (String target : p.getTargetFiles()) {
completion.submit(new PropertiesLoader(target, p));
}
for (@SuppressWarnings("unused")
String target : p.getTargetFiles()) {
Properties r = null;
try {
r = completion.take().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
p.addTargetFilesProperties(r);
}
pool.shutdown();