ExecutorService awaitTermination gets stuck

Kevin picture Kevin · Aug 14, 2011 · Viewed 10.6k times · Source

I made a fixed size thread pool with Executors.newFixedThreadPool(2), and I executed 10 Runnable objects. I set breakpoints and traced through the execution. However, fixedSizeThreadPool.awaitTermination() does not allow me to continue even though all the tasks are done.

Basically:

ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);
for (int i = 0; i < 10; ++i) {
    fixedSizeThreadPool.execute(myRunables[i]);
}
try {
    fixedSizeThreadPool.awaitTermination(timeout, timeoutUnits);
} catch (Exception e) { }
System.out.println("done!");

But this always gets stuck on awaitTermination. What's wrong?

Answer

Paul Bellora picture Paul Bellora · Aug 14, 2011

As Peter pointed out, shutdown() must be called first.

source: javadoc