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?
You are doing it right.
Lets say that
SomeCollectioncontains 100 items, and thatFileExtractortakes 5 seconds to run, and that yourExecutorServicethread pool contains 100 threads.If you start things as you have implemented above, it is expected that the code would run for around 5 seconds because
FileExtractorwould likely be I/O bound. (assuming maximum CPU efficiency).If you didn’t use
Future‘s, and everything ran serially, this code would be running for about 500 seconds instead.The key is that
Future#get()waits for the result to be populated by theThreadstarted by submitting yourCallableto theExecutorService, rather than waiting at theExecutorService#submit(Callable)method.