/** * 2019.4.20 * 无核心线程,最大数量工作线程,存活时间短。 * 可用于 一次性、大数量、高频等异步任务处理。 * @author bladeofgod */ public class CacheThreadPool { private volatile ThreadPoolExecutor mExecutors; public CacheThreadPool(){} private void initThreadPoolExecutor(){ if (mExecutors == null || mExecutors.isShutdown() || mExecutors.isTerminated()){ synchronized (CacheThreadPool.class){ if (mExecutors == null || mExecutors.isShutdown()|| mExecutors.isTerminated()){ long keepAliveTime = 60L; TimeUnit unit = TimeUnit.MILLISECONDS; mExecutors = new ThreadPoolExecutor( 0 , Integer.MAX_VALUE, keepAliveTime, unit, new SynchronousQueue<>()); } } } } public void execute(Runnable task){ initThreadPoolExecutor(); mExecutors.execute(task); } public Future submit(Runnable task){ initThreadPoolExecutor(); return mExecutors.submit(task); } public void remove(Runnable task){ initThreadPoolExecutor(); mExecutors.remove(task); } }
/** * 2019.4.20 * 指定数量的核心线程和工作线程 3秒存活时间 * 可用于一般异步任务处理 * @author bladeofgod */ public class DefaultThreadPool { private volatile ThreadPoolExecutor mExecutor; private int mCoreSize; private int mMaximumSize; public DefaultThreadPool(int mCoreSize,int mMaximumSize){ this.mCoreSize = mCoreSize; this.mMaximumSize = mMaximumSize; } private void initThreadPoolExecutor(){ if (mExecutor == null || mExecutor.isShutdown() || mExecutor.isTerminated()){ synchronized (DefaultThreadPool.class){ if (mExecutor == null || mExecutor.isShutdown() || mExecutor.isTerminated()){ long keepAliveTime = 3000; TimeUnit unit = TimeUnit.MILLISECONDS; BlockingQueue blockingQueue = new LinkedBlockingDeque(); ThreadFactory threadFactory = Executors.defaultThreadFactory(); RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy(); mExecutor = new ThreadPoolExecutor(mCoreSize, mMaximumSize, keepAliveTime, unit, blockingQueue,threadFactory,handler); } } } } public void execute(Runnable task){ initThreadPoolExecutor(); mExecutor.execute(task); } public Future submit(Runnable task){ initThreadPoolExecutor(); return mExecutor.submit(task); } public void remove(Runnable task){ initThreadPoolExecutor(); mExecutor.remove(task); } }
/** * @author lijiaqi * @date 2019/6/14. * * 单一线程线程池,一般用于单一的,线性顺序执行任务。 * * exp: 单一流水线顺序处理产品 */ public class SingleThreadPool { private volatile ExecutorService mExecutor; public SingleThreadPool() { } private void initThreadPoolExecutor(){ if (mExecutor == null || mExecutor.isShutdown() || mExecutor.isTerminated()){ synchronized (SingleThreadPool.class){ if (mExecutor == null || mExecutor.isShutdown() || mExecutor.isTerminated()){ mExecutor = Executors.newSingleThreadExecutor(); } } } } public void execute(Runnable task){ initThreadPoolExecutor(); mExecutor.execute(task); } public Future submit(Runnable task){ initThreadPoolExecutor(); return mExecutor.submit(task); } }
/** * 2019.4.20 * 线程池代理,根据不同需求返回不同类型线程池 * 可自行根据需求扩展 * @author bladeofgod */ public class ThreadPoolProxyFactory { private static volatile DefaultThreadPool defaultThreadPool; private static volatile CacheThreadPool cacheThreadPool; private static volatile SingleThreadPool singleThreadPool; public static SingleThreadPool getSingleThreadPool(){ if (singleThreadPool == null){ synchronized (ThreadPoolExecutor.class){ if (singleThreadPool == null){ singleThreadPool = new SingleThreadPool(); } } } return singleThreadPool; } public static DefaultThreadPool getDefaultThreadPool(){ if (defaultThreadPool == null){ synchronized (ThreadPoolProxyFactory.class){ if (defaultThreadPool == null){ defaultThreadPool = new DefaultThreadPool(3, 5); } } } return defaultThreadPool; } public static CacheThreadPool getCacheThreadPool(){ if (cacheThreadPool == null){ synchronized (ThreadPoolProxyFactory.class){ if (cacheThreadPool == null){ cacheThreadPool = new CacheThreadPool(); } } } return cacheThreadPool; } }
使用:
ThreadPoolProxyFactory.getCacheThreadPool().execute(new Runnable() { @Override public void run() { } });