自定义线程池工具类

该博客介绍了一个线程池工具类的实现,通过单例模式提供线程池服务。线程池配置包括核心线程数、最大线程数、等待队列长度和线程存活时间。当任务提交超出容量时,采用自定义的AbortPolicy拒绝策略,记录警告日志。此外,还提供了提交任务、获取线程池信息和取消任务的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * 线程池工具类(单例)
 */
public class MyThreadPoolUtils<T> {
    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() {
    }

    /**
     * 获取单例的线程池对象--单例的双重校验
     *
     * @return 线程池
     */
    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;
    }

    /**
     * 向线程池提交一个任务,返回线程结果
     *
     * @param callable 任务
     * @return 处理结果
     */
    public static <T> Future<T> submit(Callable<T> callable) {
        return getThreadPool().submit(callable);
    }

    /**
     * 向线程池提交一个任务,不关心处理结果
     *
     * @param runnable 任务
     */
    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);
        }
    }

}
### C语言实现合并两个有序数组 在C语言中,可以采用双指针方法来高效地合并两个已排序的数组。这种方法利用了输入数组已经排序的特点,在不额外占用大量空间的情况下完成合并操作。 对于给定的任务——将`nums2`合并入`nums1`并保持其非递减顺序排列,可以从两个数组的有效部分末端开始向前遍历比较,并逐步填充至`nums1`的尾部位置[^5]。 下面展示一段具体的代码示例: ```c void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){ int end1 = m - 1; // 指向第一个数组最后一个有效元素的位置 int end2 = n - 1; // 指向第二个数组最后一个有效元素的位置 int end = m + n - 1; // 指向合并数组应放置下一个较大值的位置 while (end1 >= 0 && end2 >= 0) { if (nums1[end1] > nums2[end2]) { nums1[end--] = nums1[end1--]; } else { nums1[end--] = nums2[end2--]; } } // 如果num2还有剩余,则全部复制过来;因为如果此时有任一数组未处理完毕, // 剩下的一定是较小者,而这些较小者的原始位置已经在正确的地方(即nums1前面) while(end2 >= 0){ nums1[end--] = nums2[end2--]; } } ``` 此函数接收五个参数:目标数组`nums1`及其大小`nums1Size`、实际长度`m`;源数组`nums2`及其大小`nums2Size`、实际长度`n`。通过调整索引来避免越界访问的同时完成了两数组合并工作[^4]。 该算法的时间复杂度为O(m+n),其中m和n分别是两个输入数组的实际长度。这是因为每个元素最多只会被访问一次。此外,由于是在原地修改`nums1`,因此不需要额外的空间开销,除了几个用于追踪进度的变量外[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值