動態創建線程池參數
/**
*獲取當前機器CPU數量
*/
private static final int CPU = Runtime.getRuntime().availableProcessors();
/**
*核心線程數(默認線程數)
*/
private static final int CORE_POOL_SIZE = CPU;
/**
*最大線程數
*/
private static final int MAX_POOL_SIZE = CPU * 2;
/**
*允許線程空閒時間(單位:默認爲秒)
*/
private static final int KEEP_ALIVE_TIME = 60;
/**
*緩衝隊列數
*/
private static final int QUEUE_CAPACITY = 200;
/**
*線程池名前綴
*/
private static final String THREAD_NAME_PREFIX = "wflTaskExecutor-";
創建線程池使用ThreadPoolExecutor
/**
* 核心線程 CORE_POOL_SIZE 最大線程 MAX_POOL_SIZE 保活時間 KEEP_ALIVE_TIME
* 存儲隊列 QUEUE_CAPACITY 有守護線程 拒絕策略:將超負荷任務回退到調用者
*/
private static ExecutorService executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
KEEP_ALIVE_TIME, TimeUnit.SECONDS, new LinkedBlockingQueue<>(QUEUE_CAPACITY),
new ThreadFactoryBuilder().setNameFormat(THREAD_NAME_PREFIX).setDaemon(true).build(),
new ThreadPoolExecutor.CallerRunsPolicy());