WIZARDELF
WIZARDELF

Reputation: 3895

shutdowning a fixed size thread pool

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

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

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);

ExecutorService.shutdown():

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

Related Questions