ctl来表示运行状态和工作线程数
线程池的优雅关闭
@Component
public class MyContextClosedHandler implements ApplicationListener<ContextClosedEvent>{
@Autowired
@Qualifier("backExecutor")
private ThreadPoolTaskExecutor backExecutor;
private static int WAIT_TIME = 30;
@Override
public void onApplicationEvent(ContextClosedEvent event) {
shutdownAndAwaitTermination(backExecutor.getThreadPoolExecutor());
}
private void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown();
// Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {
pool.shutdownNow();
// Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(30, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
}
catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}
springboot-35-线程池优雅停机secvtorii新浪博客
线程池处理异常
Thread&ThreadPoolExecutor异常处理