单列线程池

这是一个关于Java实现单列线程池的工具类,用于处理异步任务。线程池核心线程数为10,最大线程数为15,空闲线程回收时间为5秒,使用LinkedBlockingDeque作为工作队列,并通过defaultThreadFactory创建线程。提供静态方法获取线程池实例,执行和取消任务。

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

package com.vivo.finance.vivopay.core.utils;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**

  • @author liusenxiao

  • @description 单列线程池异步任务处理器

  • @data2019/12/11
    */
    public class ThreadPoolServiceUtil {
    // 核心线程数
    private static final int DEFAULT_CORE_SIZE=10;
    // 最大线程数
    private static final int MAX_QUEUE_SIZE=15;
    private volatile static ThreadPoolExecutor executor;

    /** 空闲线程回收时间 */
    public static final int KEEP_ALIVE_TIME = 5000;

    private ThreadPoolServiceUtil() {};

    // 获取单例的线程池对象
    public static ThreadPoolExecutor getInstance() {
    if (executor == null) {
    synchronized (ThreadPoolServiceUtil.class) {
    if (executor == null) {
    executor = new ThreadPoolExecutor(DEFAULT_CORE_SIZE,
    MAX_QUEUE_SIZE,
    KEEP_ALIVE_TIME,
    TimeUnit.MILLISECONDS,// 时间单位
    new LinkedBlockingDeque(Integer.MAX_VALUE),// 线程队列
    Executors.defaultThreadFactory()// 线程工厂
    );
    }
    }
    }
    return executor;
    }

    public void execute(Runnable runnable) {
    if (runnable == null) {
    return;
    }
    executor.execute(runnable);
    }

    // 从线程队列中移除对象
    public void cancel(Runnable runnable) {
    if (executor != null) {
    executor.getQueue().remove(runnable);
    }
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值