public class ThreadPoolExecutorUtil {
/**
* corePoolSize 池中所保存的线程数,包括空闲线程。
*/
private static final int corePoolSize = 20;
/**
* maximumPoolSize - 池中允许的最大线程数(采用LinkedBlockingQueue时没有作用)。
*/
private static final int maximumPoolSize = 50;
/**
* keepAliveTime -当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间,线程池维护线程所允许的空闲时间
*/
private static final int keepAliveTime = 30;
/**
* 执行前用于保持任务的队列(缓冲队列)
*/
private static final int capacity = 300;
/**
* 线程池对象
*/
private static ThreadPoolExecutor threadPoolExecutor = null;
/**
* 构造方法私有化
*/
private ThreadPoolExecutorFactoryUtil() {
}
public static ThreadPoolExecutor getThreadPoolExecutor() {
if (null == threadPoolExecutor || threadPoolExecutor.getPoolSize() == 0) {
ThreadPoolExecutor t;
synchronized (ThreadPoolExecutor.class) {
t = threadPoolExecutor;
if (null == t || threadPoolExecutor.getPoolSize() == 0) {
synchronized (ThreadPoolExecutor.class) {
t = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
}
threadPoolExecutor = t;
}
}
}
return threadPoolExecutor;
}
}
线程池工具类
最新推荐文章于 2024-03-18 16:48:16 发布