码迷,mamicode.com
首页 > 编程语言 > 详细

线程池的使用

时间:2019-12-17 20:39:55      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:OLE   dfa   adp   set   read   ice   ==   zed   linked   

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

线程池的使用

标签:OLE   dfa   adp   set   read   ice   ==   zed   linked   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!