首页 > 编程语言 > 详细

线程池的使用

时间:2019-12-17 19:55:28      阅读:85      评论:0      收藏:0      [点我收藏+]
public class ThreadPoolExecutorUtils {

    private static final int CORE_POOL_SIZE = 10;
    private static final int MAX_POOL_SIZE = 20;
    private static final long KEEP_ALIVE_TIME = 30;
    private static final int QUEUE_SIZE = 20;
    private static final String THREAD_POOL_NAME = "thread-pool";
    private static volatile ExecutorService pool = null;

    private ThreadPoolExecutorUtils(){

    }
    public static ExecutorService getPool() {
        // 使用双重校验锁保证只有一个pool实例
        if (pool == null) {
            synchronized (ThreadPoolExecutorUtils.class) {
                if (pool == null) {
                    ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat(THREAD_POOL_NAME + "-%d").build();
                    pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(QUEUE_SIZE), namedThreadFactory,
                            new ThreadPoolExecutor.AbortPolicy());
                }
            }
        }
        return pool;
    }
}

线程池的使用

原文:https://www.cnblogs.com/tilamisu007/p/12056294.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!