java LimitedThreadPool

此线程池一直增长,直到上限增长后不收缩(因为池子里面的线程是永生的)。这个keepAliveTime参数设置的为Long.MAX_VALUE,所以池子里面的线程几乎不会因为idle而被terminate,也就是说只要线程被创建并放到池子里面永远不会被销毁,永生。

    /**
     * 此线程池一直增长,直到上限,增长后不收缩。
     * */
    public class LimitedThreadPool implements ThreadPool {
        public static final int DEFAULT_CORE_THREADS = 0;
        public static final int DEFAULT_THREADS = 200;
        public static final int DEFAULT_QUEUES = 0;

        public Executor getExecutor(URL url) {
            String name = url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME);
            int cores = url.getParameter(Constants.CORE_THREADS_KEY, Constants.DEFAULT_CORE_THREADS);
            int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS);
            int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
            return new ThreadPoolExecutor(cores, threads, Long.MAX_VALUE, TimeUnit.MILLISECONDS,
                    queues == 0 ? new SynchronousQueue<Runnable>()
                            : (queues < 0 ? new LinkedBlockingQueue<Runnable>()
                                    : new LinkedBlockingQueue<Runnable>(queues)),
                    new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url));
        }

    }

 

public interface ThreadPool {
    
    /**
     * 线程池
     * 
     * @param url 线程参数
     * @return 线程池
     */
    @Adaptive({Constants.THREADPOOL_KEY})
    Executor getExecutor(URL url);

}

 

    原文作者:FrankYou
    原文地址: https://www.cnblogs.com/frankyou/p/9525286.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞