【发布时间】:2012-01-31 08:27:20
【问题描述】:
我在一个spring bean中创建了一个threadpoolexecutor,所以我需要在tomcat关闭时关闭这个executor。
public class PersistBO implements InitializingBean {
private ThreadPoolExecutor executer;
public void shutdownExecutor() {
executer.shutdown();
}
@Override
public void afterPropertiesSet() throws Exception {
taskQueue = new LinkedBlockingQueue<Runnable>(queueLength);
executer = new ThreadPoolExecutor(minThread, maxThread, threadKeepAliveTime,
TimeUnit.SECONDS, taskQueue, new ThreadPoolExecutor.DiscardOldestPolicy());
}
我在谷歌上搜索了解决方案并得到了结果。也就是给java.lang.runtime加一个shutdownhook。但是,java 文档说 java.lang.Runtime#shutdownHook 在最后一个非守护线程退出时被调用。所以这是一个死锁。 spring bean中关闭执行器有什么解决方案吗?
【问题讨论】:
-
如果您提供正确的线程工厂,您的池也可以使用守护线程。
标签: java spring javabeans shutdown