Reputation: 3895
I use Executors.newFixedThreadPool
to generate a thread pool, and want to shutdown it when the job queue is empty and no thread is working. How should I do that?
Upvotes: 1
Views: 100
Reputation: 340903
You first need to shutdown your pool while letting all already submitted tasks to finish but not allowing new ones. Then you can block until the queue is empty and all tasks have completed:
pool.shutdown();
pool.awaitTermination(1, TimeUnit.HOUR);
previously submitted tasks are executed, but no new tasks will be accepted
ExecutorService.awaitTermination()
:
Blocks until all tasks have completed execution after a shutdown request
Upvotes: 4