package com.bonade.utils; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 每次获取生成新的线程池 * @author xiejianwei * @description 适用于有状态的线程处理 */ public class PrototypeThreadPoolUtil { //根据cpu的数量动态的配置核心线程数和最大线程数 private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); //核心线程数 = CPU核心数 + 1 private static final int CORE_POOL_SIZE = CPU_COUNT + 1; //线程池最大线程数 = CPU核心数 * 2 + 1 private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 10; //非核心线程闲置时间 = 超时1s private static final int KEEP_ALIVE = 1; // 队列容量 private static final int CAPACITY = 500; public static ThreadPoolExecutor getInstance() { return new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); } }
线程池工具类:PrototypeThreadPoolUtil
最新推荐文章于 2023-07-23 16:21:54 发布