在這個例子中,我提交了幾個文件到我的比較對象。它一切正常,但我注意到提交文件的順序並不總是與它們返回的順序相同。有關如何更好地控制此問題的任何建議?如何控制Java期貨被「提交」的訂單?
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();