枚举单例法实现线程池

利用构造器实现枚举

代码示例

package com.joinf.dbutils.client.service.busi.cs.pool;

import cn.hutool.core.thread.NamedThreadFactory;

import java.util.concurrent.*;

/**
 * @author lyf
 * @date 2021/11/11
 * @description
 */
public enum ThreadPool {

    INSTANCE;

    private ThreadPoolExecutor threadPool;

    public ThreadPoolExecutor getInstance() {
        return this.threadPool;
    }

    /**
     * 无返回值直接执行
     */
    public void execute(Runnable runnable) {
        this.threadPool.execute(runnable);
    }

    public ThreadPoolExecutor executor() {
        return this.threadPool;
    }

    /**
     * 返回值直接执行
     */
    public <T> Future<T> submit(Callable<T> callable) {
        return this.threadPool.submit(callable);
    }

    ThreadPool() {

        int CPU_COUNT = Runtime.getRuntime().availableProcessors();

        // 核心线程数 = CPU核心数 + 1
        int CORE_POOL_SIZE = CPU_COUNT + 1;

        // 线程池最大线程数 = CPU核心数 * 2 + 1
        int MAX_POOL_SIZE = CPU_COUNT * 2 + 1;

        // 非核心线程闲置时超时
        int KEEP_ALIVE = 60;

        this.threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(200000), new NamedThreadFactory("cs-data-import-", false));

    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            ThreadPoolExecutor executor = ThreadPool.INSTANCE.getInstance();
            System.out.println(executor);
        }
    }

}

执行结果

java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]

利用抽象方法实现枚举(失败案例)

package com.joinf.dbutils.client.service.busi.cs.pool;

import cn.hutool.core.thread.NamedThreadFactory;

import java.util.concurrent.*;

/**
 * @author lyf
 * @date 2021/11/11
 * @description
 */
public enum ThreadPool {

    INSTANCE{
        @Override
        public ThreadPoolExecutor newInstance() {

            int CPU_COUNT = Runtime.getRuntime().availableProcessors();

            // 核心线程数 = CPU核心数 + 1
            int CORE_POOL_SIZE = CPU_COUNT + 1;

            // 线程池最大线程数 = CPU核心数 * 2 + 1
            int MAX_POOL_SIZE = CPU_COUNT * 2 + 1;

            // 非核心线程闲置时超时
            int KEEP_ALIVE = 60;

            return new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(200000), new NamedThreadFactory("cs-data-import-", false));
            
        }
    };
    
    public abstract ThreadPoolExecutor newInstance();

    /**
     * 无返回值直接执行
     */
    public void execute(Runnable runnable) {
        INSTANCE.newInstance().execute(runnable);
    }

    /**
     * 返回值直接执行
     */
    public <T> Future<T> submit(Callable<T> callable) {
        return INSTANCE.newInstance().submit(callable);
    }

    ThreadPool() {
    }

}

java.util.concurrent.ThreadPoolExecutor@ba4d54[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@12bc6874[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@de0a01f[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@4c75cab9[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@1ef7fe8e[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@6f79caec[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@67117f44[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@5d3411d[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@2471cca7[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@5fe5c6f[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容