美文网首页
java线程池配置

java线程池配置

作者: writeanewworld | 来源:发表于2021-07-17 16:12 被阅读0次
    
    /**
     * 线程池执行器工厂.
     */
    public final class ThreadPoolExecutorFactory {
    
        private ThreadPoolExecutorFactory() {
        }
    
        public static ThreadPoolExecutor threadPoolExecutor() {
            return ThreadPoolExecutorHolder.threadPoolExecutor;
        }
    
        public static void execute(@NonNull Runnable runnable) {
            ThreadPoolExecutorHolder.threadPoolExecutor.execute(runnable);
        }
    
        private static class ThreadPoolExecutorHolder {
            static final int cpu = Runtime.getRuntime().availableProcessors();
            static final int corePoolSize = cpu + 1;
            static final int maximumPoolSize = cpu * 2 + 1;
            static final long keepAliveTime = 1L;
            static final TimeUnit timeUnit = TimeUnit.SECONDS;
            static final int maxQueueNum = 1024;
    
            public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                    corePoolSize, maximumPoolSize, keepAliveTime, timeUnit,
                new LinkedBlockingQueue<>(maxQueueNum),
                new NamedThreadFactory("ThreadPoolExecutorFactory-", false),
                    new ThreadPoolExecutor.AbortPolicy());
        }
    
    

    相关文章

      网友评论

          本文标题:java线程池配置

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