线程被突然终止(强杀)了,没关闭的线程池怎么办?
如果线程被强杀之后有相应的回调方法,那么可以进行重新初始化线程池的操作。下面展示一下伪代码:
public class ThreadPoolHelper {
/**
* 重新初始化指定线程池
*/
public void resetThreadPool(String threadPoolName){
if(StringUtils.isBlank(threadPoolName)){
return;
}
Collection<ThreadPoolInfo> threadPoolInfos = threadPoolConfig.getThreadPoolConfig();
for(ThreadPoolInfo threadPoolInfo:threadPoolInfos){
if(threadPoolInfo.getName().equals(threadPoolName)){
// shutdown old threadpool
ExecutorService threadPoolOld = multiThreadPool.get(threadPoolInfo.getName());
if(threadPoolOld!=null){
shutdownAndAwaitTermination(threadPoolName,threadPoolOld);
}
// re-init threadPool
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(threadPoolInfo.getQueueSize());
ThreadPoolExecutor executor = new ThreadPoolExecutor(
threadPoolInfo.getCoreSize(),threadPoolInfo.getMaxSize(),
threadPoolInfo.getThreadKeepAliveTime(),TimeUtil.SECONDS,
workQueue,new DefaultThreadFactory(threadPoolInfo.getName(),
new RejectedExecutionHandlerImpl())
);
multiThreadPool.put(threadPoolInfo.getName(),threadPoolInfo);
}
}
}
}
网友评论