我有請求列表,我需要在線程中發送它們,並在線程完成時收集所有響應。我怎麼知道所有的步驟都完成了?ExecutionService。所有線程完成後如何獲得結果?
List<Task<Response>> tasks = new ArrayList<Task<Response>>();
for (Request request : requestList) {
tasks.add(executor.runTask(new RequestCaller(request))); // runTask is my method witch will run threads
}
for (int i = 0; i < tasks.size; i++) {
result.add(tasks.get(i).get); // here i want to collect results, but i need to run this loop only when all threads will finish
}