线程池的使用
Posted tilamisu007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程池的使用相关的知识,希望对你有一定的参考价值。
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; } }
以上是关于线程池的使用的主要内容,如果未能解决你的问题,请参考以下文章