MinerThreadPool.java 线程池
package com.iteye.injavawetrust.miner; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 线程池 * @author InJavaWeTrust * */ public class MinerThreadPool { private static volatile ThreadPoolExecutor executor = null; /** * 核心池的大小 */ private static int corePoolSize = 100; /** * 线程池最大线程数 */ private static int maximumPoolSize = 200; /** * 线程没有任务执行时最多保持多久时间会终止 */ private static long keepAliveTime = 100L; /** * 参数keepAliveTime的时间单位 */ private static TimeUnit unit = TimeUnit.SECONDS; private MinerThreadPool() { } public static ThreadPoolExecutor getInstance() { if (null == executor) { executor = getInstance(corePoolSize, maximumPoolSize, keepAliveTime, unit); } return executor; } public static ThreadPoolExecutor getInstance(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit){ if (null == executor) { synchronized (MinerThreadPool.class) { BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, queue); } } return executor; } /** * shutdown */ public static void shutdown() { if (executor != null) { executor.shutdown(); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步