Reputation: 96391
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?
Upvotes: 2
Views: 548
Reputation: 51214
Each future is started as soon as you submit it, by calling pool.submit
. If your main thread had some other work to do in the meantime (between starting futures, and waiting for them to end), it could easily happen that all your futures are done by the time you got to the second loop, so there would practically be no blocking.
And if the get
method does block the main thread, while you are waiting for the first Future
to finish, note that others are still running parallely (at least potentially, given enough processor time).
Upvotes: 2
Reputation: 42849
You are doing it right.
Lets say that SomeCollection
contains 100 items, and that FileExtractor
takes 5 seconds to run, and that your ExecutorService
thread 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 FileExtractor
would 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 the Thread
started by submitting your Callable
to the ExecutorService
, rather than waiting at the ExecutorService#submit(Callable)
method.
Upvotes: 4