欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

自定义线程池工具类

时间:2023-08-01

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.concurrent.*;public class MyThreadPoolUtils { private static final Logger LOGGER = LoggerFactory.getLogger(MyThreadPoolUtils.class); // 等待队列长度 private static final int BLOCKING_QUEUE_LENGTH = 1000; // 闲置线程存活时间 private static final int KEEP_ALIVE_TIME = 60; // 闲置线程存活时间单位 private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS; // 线程名称 private static final String THREAD_NAME = "myPool"; // 线程池执行器 private static ThreadPoolExecutor executor; // 私有化构造子,阻止外部直接实例化对象 private MyThreadPoolUtils() { } public static ThreadPoolExecutor getThreadPool() { if (executor == null) { synchronized (MyThreadPoolUtils.class) { if (executor == null) { // 获取处理器数量 int cpuNum = Runtime.getRuntime().availableProcessors(); // 根据cpu数量,计算出合理的线程并发数 int maximumPoolSize = cpuNum * 2 + 1; // executor = new ThreadPoolExecutor( // 核心线程数 maximumPoolSize - 1, // 最大线程数 maximumPoolSize, // 活跃时间 KEEP_ALIVE_TIME, // 活跃时间单位 KEEP_ALIVE_TIME_UNIT, // 线程队列 new linkedBlockingDeque<>(BLOCKING_QUEUE_LENGTH), // 线程工厂 Executors.defaultThreadFactory(), // 队列已满,而且当前线程数已经超过最大线程数时的异常处理策略(这里可以自定义拒绝策略) new ThreadPoolExecutor.AbortPolicy() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { LOGGER.warn("线程等待队列已满,当前运行线程总数:{},活动线程数:{},等待运行任务数:{}", e.getPoolSize(), e.getActiveCount(), e.getQueue().size()); } } ); } } } return executor; } public static Future submit(Callable callable) { return getThreadPool().submit(callable); } public static void execute(Runnable runnable) { if (runnable == null) throw new NullPointerException(); getThreadPool().execute(runnable); } public static int getSize() { return getThreadPool().getPoolSize(); } public static int getActiveCount() { return getThreadPool().getActiveCount(); } public static void cancel(Runnable runnable) { if (executor != null) { getThreadPool().getQueue().remove(runnable); } }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。