线程池配置类

ThreadPoolTaskConfig:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
@ConfigurationProperties(prefix = "executor.thread")
public class ThreadPoolTaskConfig{
    
  private static final Logger logger = LoggerFactory.getLogger(ThreadPoolTaskConfig.class);

  private Integer corePoolSize;
  private Integer maxPoolSize;
  private Integer keepAliveSeconds;
  private Integer queueCapacity;
  private String threadNamePrefix;


  @Bean(name = "threadPoolTask")
  public Executor executor(){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    //核心线程数
    executor.setCorePoolSize(this.corePoolSize);
    //最大线程数
    executor.setMaxPoolSize(this.maxPoolSize);
    //核心线程外的线程存活时间秒
    executor.setKeepAliveSeconds(this.keepAliveSeconds);
    //队列大小
    executor.setQueueCapacity(this.queueCapacity);
    //线程名称前缀
    executor.setThreadNamePrefix(this.threadNamePrefix);
    //拒绝策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallRunsPolicy());
    executor.initializer();
    logger.info("=======线程池创建成功=======");
    return executor;
    }
    

    //get set方法,手打太累了,记得补全下;
    public Integer getCorePoolSize() {return corePoolSize;}

    public void setCorePoolSize(Integer corePoolSize) {this.corePoolSize=corePoolSize;}

}

yml文件配置:

###################
#     线程池
###################
executor:
    thread:
        core-pool-size: 25
        max-pool-size: 30
        keep-alive-seconds: 300
        queue-capacity: 5
        thread-name-prefix: thread-name

使用线程池:

//针对你业务需要用到的方法添加注解@Async
//例:

@Async(value = "threadPoolTask")
public void word(Map<String,Object> map){
   


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java多模块项目中共用一个线程池配置,可以将线程池配置代码写在一个公共的工具类中,然后在各个模块中调用该工具类即可。 具体实现步骤如下: 1.创建一个公共的线程池配置工具类Configure,例如: ```java public class Configure { private static final int CORE_POOL_SIZE = 10; private static final int MAX_POOL_SIZE = 10; private static final long KEEP_ALIVE_TIME = 0L; private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS; private static final BlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingQueue<>(); private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, WORK_QUEUE); static { executorService.allowCoreThreadTimeOut(true); } public static ThreadPoolExecutor getExecutorService() { return executorService; } } ``` 2.在需要使用线程池的模块中,调用该工具类获取线程池对象,例如: ```java public class MyModule { private static ThreadPoolExecutor executorService = Configure.getExecutorService(); public void doSomething() { executorService.execute(new Runnable() { @Override public void run() { // 执行线程任务 } }); } } ``` 通过上述方式,不同的模块均可共用同一个线程池配置,实现代码复用和统一管理。注意,使用ThreadPoolExecutor相较于Executors创建线程池,可以更加灵活地配置线程池参数。此外,在Configure类中添加了static代码块来进行线程池的初始化操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值