@Slf4j
public class ThreadPoolExecutorForIOUtil {
private static ThreadPoolExecutor poolExecutor;
private static volatile ThreadPoolExecutorForIOUtil instance;
private ThreadPoolExecutorForIOUtil(){
int processNum = Runtime.getRuntime().availableProcessors();
log.info("自定义IO密集型线程池获取CPU核数为:{}", processNum);
poolExecutor = new ThreadPoolExecutor(2 * processNum,
2 * processNum,
60,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
poolExecutor.allowCoreThreadTimeOut(true);
}
public static ThreadPoolExecutorForIOUtil getInstance(){
if (instance == null) {
synchronized (ThreadPoolExecutorForIOUtil.class) {
if (instance == null) {
instance = new ThreadPoolExecutorForIOUtil();
}
}
}
return instance;
}
public <T> Future<T> sumbit(Callable<T> task){
return poolExecutor.submit(task);
}
public void execute(Runnable runnable){
poolExecutor.execute(runnable);
}
}
IO型线程池工具类
最新推荐文章于 2024-09-05 11:00:53 发布