java线程池配置


/**
 * 线程池执行器工厂.
 */
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());
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容