I have the following code:
public class AbcThreadPool {
private final AbcThreadPoolExecutor executor;
public InventoryAvailabilityThreadPool(final AbcRunnableFactory factory,
final Integer poolSize) {
executor = new AbcThreadPoolExecutor(factory, poolSize);
for (int i = 0; i < poolSize; ++i) {
executor.execute(factory.createRunnable());
}
}
private static class AbcThreadPoolExecutor extends ThreadPoolExecutor {
private final AbcRunnableFactory factory;
public AbcThreadPoolExecutor(final AbcRunnableFactory factory,
final int poolSize) {
super(poolSize, poolSize, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
this.factory = factory;
allowCoreThreadTimeOut(false);
}
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
if (!isShutdown() && !Thread.currentThread().isInterrupted()) {
execute(factory.createRunnable());
}
}
}
In this code, will it handle the case that if the shutdown of the process happens say by Ctrl + C, it would wait for termination of threads before killing it? Or I need to add a shutdownHook for doing so to call
executorService.awaitTermination
before the program exits?