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

}

 

posted @ 2018-08-23 17:48  FrankYou  阅读(415)  评论(0编辑  收藏  举报