美文网首页
线程被突然终止了,没关闭的线程池怎么办?

线程被突然终止了,没关闭的线程池怎么办?

作者: 不知名的蛋挞 | 来源:发表于2018-12-08 20:01 被阅读12次

线程被突然终止(强杀)了,没关闭的线程池怎么办?

如果线程被强杀之后有相应的回调方法,那么可以进行重新初始化线程池的操作。下面展示一下伪代码:

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);
            }
        }

    }
}

相关文章

网友评论

      本文标题:线程被突然终止了,没关闭的线程池怎么办?

      本文链接:https://www.haomeiwen.com/subject/gddrqqtx.html