spring boot 之 ThreadPoolTaskExecutor线程池

线程池配置

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 具体问题具体分析,比较理想的方案,仅供参考
 * 计算密集型: 线程数 = CPU核数 + 1,也可以设置成CPU核数*2,一般设置CPU*2
 * IO密集型: 线程数 = CPU核心数/(1-阻塞系数),这个组赛系数一般为0.8~0.9之间,也可以取0.8或者0.9
 */
@Slf4j
@EnableAsync
@Configuration
public class ThreadPoolConfig {

    // 获取服务器的cpu个数
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    // 核心线程数量
    private static final int COUR_SIZE = CPU_COUNT * 2;
    // 线程最大数量
    private static final int MAX_COUR_SIZE = COUR_SIZE * 4;

    @Bean("threadExecutor")
    public ThreadPoolTaskExecutor threadExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = null;
        try {
            threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
            // 获取CPU核心数
            int i = Runtime.getRuntime().availableProcessors();
            //核心线程数目
            threadPoolTaskExecutor.setCorePoolSize(COUR_SIZE);
            //指定最大线程数
            threadPoolTaskExecutor.setMaxPoolSize(MAX_COUR_SIZE);
            //队列中最大的数目
            threadPoolTaskExecutor.setQueueCapacity(MAX_COUR_SIZE * 2 * 10);
            //线程空闲后的最大存活时间
            threadPoolTaskExecutor.setKeepAliveSeconds(60);
            //线程名称前缀
            threadPoolTaskExecutor.setThreadNamePrefix("threadExecutor-");
            //拒绝策略
            threadPoolTaskExecutor.setRejectedExecutionHandler(new SelfRejectedExecutionHandler());
            //当调度器shutdown被调用时等待当前被调度的任务完成
            threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
            //加载
            threadPoolTaskExecutor.initialize();
            log.info("初始化线程池成功");
        } catch (Exception e) {
            log.error("初始化线程池失败: {}", e.getMessage());
        }
        return threadPoolTaskExecutor;
    }
}

线程任务存取工具类

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
@Component
public class ThreadReader {

    private static final BlockingQueue<Runnable> BLOCKING_QUEUE = new LinkedBlockingQueue<>();

    @Autowired
    @Qualifier("threadExecutor")
    private ThreadPoolTaskExecutor threadExecutor;

    public static void put(Runnable runnable) {
        try {
            BLOCKING_QUEUE.put(runnable);
        } catch (InterruptedException e) {
            log.error("ThreadReader.put异常:{}", e.getMessage());
        }
    }

    public void take() {
        if (CollectionUtils.isEmpty(BLOCKING_QUEUE)) {
            return;
        }
        try {
            Runnable runnable = BLOCKING_QUEUE.take();
            log.info("取出当前线程池没来得及执行的任务, runnable:{}", runnable);
            threadExecutor.execute(runnable);
        } catch (InterruptedException e) {
            log.error("ThreadReader.take异常:{}", e.getMessage());
        }
    }
}

自定义线程池的拒绝策略

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class SelfRejectedExecutionHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
        if (null != runnable) {
            // 线程池没来得及执行的任务先放入队列
            ThreadReader.put(runnable);
        }
    }
}

重新执行未执行的任务

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class CustomizeScheduler implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private ThreadReader threadReader;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 线程工厂
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("scheduledExecutor-pool-%d")
                .setUncaughtExceptionHandler((thread, throwable) -> log.error("ThreadPool {} got exception", thread, throwable)).build();
        ScheduledExecutorService scheduledExecutor = new ScheduledThreadPoolExecutor(1, threadFactory, new SelfRejectedExecutionHandler());
        // 之后每隔1秒执行队列中没有来得及执行的任务
        scheduledExecutor.scheduleAtFixedRate(() -> threadReader.take(), 1, 1000, TimeUnit.MILLISECONDS);
    }
}
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于Spring Boot整合ThreadPoolTaskExecutor线程池的问题,我可以给你一些基本的指导。 首先,在Spring Boot项目中,你可以通过在配置类中创建一个ThreadPoolTaskExecutor bean来整合线程池。可以遵循以下步骤: 1. 创建一个配置类,比如`ThreadPoolConfig`。 2. 在配置类中,使用`@Configuration`注解标记该类为配置类。 3. 添加一个方法,用于创建并配置`ThreadPoolTaskExecutor`实例的bean。可以使用`@Bean`注解标记该方法。 ```java @Configuration public class ThreadPoolConfig { @Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); // 设置核心线程数 executor.setMaxPoolSize(20); // 设置最大线程数 executor.setQueueCapacity(100); // 设置队列容量 executor.setThreadNamePrefix("my-thread-"); // 设置线程名前缀 executor.initialize(); // 初始化线程池 return executor; } } ``` 4. 在需要使用线程池的地方,可以使用`@Autowired`注解将`ThreadPoolTaskExecutor`注入到相关的类中。然后就可以使用线程池执行异步任务了。 ```java @Service public class MyService { @Autowired private ThreadPoolTaskExecutor executor; public void doAsyncTask() { executor.execute(() -> { // 异步任务逻辑 }); } } ``` 这样,你就成功地将`ThreadPoolTaskExecutor`线程池整合到Spring Boot项目中了。需要注意的是,根据你的需求,你可以根据实际情况调整线程池的参数,比如核心线程数、最大线程数、队列容量等。 希望这些信息对你有所帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值