package cn.huge.base.common.utils;
|
|
import java.util.concurrent.*;
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
/**
|
* @title: 线程池工具类
|
* @description: 线程池工具类
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
public class ThreadPoolUtils {
|
/**
|
* 获取处理器数量
|
*/
|
private static final int CPUNUM = Runtime.getRuntime().availableProcessors();
|
|
//private static final int MAXMUNPOOLSIZE = 10;
|
/**
|
* 线程池最大容纳线程数,通过处理器数量来确定最佳线程数量的大小
|
*/
|
private static final int MAXMUNPOOLSIZE = 2*CPUNUM+1;
|
|
//private static final int COREPOOLSIZE = 5;
|
/**
|
* 核心线程数量大小
|
*/
|
private static final int COREPOOLSIZE = 2*CPUNUM;
|
|
/**
|
* 线程空闲后的存活时长
|
*/
|
private static final int KEEPALIVETIME = 30;
|
|
private static ThreadPoolExecutor executor = null;
|
|
private static ThreadPoolExecutor getExecutor(){
|
if(executor == null){
|
synchronized (ThreadPoolExecutor.class){
|
if(executor == null){
|
executor = new ThreadPoolExecutor(
|
COREPOOLSIZE,
|
MAXMUNPOOLSIZE,
|
KEEPALIVETIME,
|
//KeepAliveTime的时间单位(秒)
|
TimeUnit.SECONDS,
|
//任务过多时存储任务的一个阻塞队列
|
//new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE),
|
new LinkedBlockingDeque<Runnable>(500),
|
new ThreadFactory() {
|
private final AtomicInteger atomicInteger = new AtomicInteger(1); //初始计数为0
|
@Override
|
public Thread newThread(Runnable runnable) {
|
// getAndIncrement以原子的操作递增当前值并返回旧值。相当于++i操作
|
return new Thread(runnable,"ThreadPoolUtil-" + atomicInteger.getAndIncrement());
|
}
|
}, //线程的创建工厂
|
//线程池任务满载后采取的任务拒绝策略
|
//new ThreadPoolExecutor.DiscardOldestPolicy()
|
//线程池满载后采取由提交任务者执行这个任务的策略
|
new ThreadPoolExecutor.CallerRunsPolicy()
|
);
|
//设置核心线程空闲时间超过keepAliveTime值时释放线程
|
executor.allowCoreThreadTimeOut(true);
|
}
|
}
|
}
|
return executor;
|
}
|
|
/**
|
* 无返回值直接执行
|
* @param runnable
|
*/
|
public static void executor(Runnable runnable){
|
getExecutor().execute(runnable);
|
}
|
|
|
/**
|
* 有返回值
|
* @param callable
|
* @param <T>
|
* @return
|
*/
|
public static <T> Future<T> submit(Callable<T> callable){
|
return getExecutor().submit(callable);
|
}
|
}
|