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

你可能感兴趣的:(java线程池配置)