Implementing Threads via Futures
-
29-10-2019 - |
Question
I wonder if the following would be a correct implementation
ExecutorService pool = Executors.newFixedThreadPool(MAX_NSH_THREADS);
Set<Future<Void>> futureRequest = new HashSet<Future<Void>>();
for (String host : SomeCollection)) {
Callable<Void> callable = new FileExtractor(j);
Future<Void> future = pool.submit(callable);
futureRequest.add(future);
}
for (Future<Void> future : futureRequest) {
try {
future.get();
} catch (Exception e) {
logger.error(e);
}
}
pool.shutdown();
According to Javadoc, future.get()
waits for execution to complete for each thread, which (as i understand it) means that for each of the future, we will wait to receive the results separately. Where is the benefit coming from then, or am i not doing it right?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow