Reputation: 205
I want to use a common thread pool among various reader objects. Each reader will read asynchronously using a thread from the pool. Here is how I define my executor :
public static ExecutorService executor = Executors.newFixedThreadPool();
I want to use it in TableReader objects. For example, there is a function readRows which will look like this :
executor.submit(new AsyncRowReader(...));
However, I am not sure how to handle the shutdown() step. The TableReader is part of a jar which can be used like this :
TableReader r = new TableReader(...);
while (not done) {
r.readRow(...);
}
There could be multiple TableReader objects from a single application. I can shutdown the executor service once all of these are done. How can I detect that there no more tasks running? Is there a different type of thread pool I can use?
Upvotes: 1
Views: 999
Reputation: 6723
Sounds like you need an ExecutorCompletionService:
ExecutorCompletionService completionService = new ExecutorCompletionService(executor);
while (completionService.poll() != null) {
System.out.println("Still working...");
}
executor.shutdown();
Upvotes: 1