自定义线程池

1.1 配置线程池

1、在公共模块配置自定义线程池
@EnableConfigurationProperties(value=ThreadPoolProperties.class)
@Configuration
publicclassThreadPoolConfiguration {
​
    //方式一
    
    // 自定义线程池
    @Bean
    publicThreadPoolExecutorthreadPoolExecutor() {
        returnnewThreadPoolExecutor(4 , 10 , 30 , TimeUnit.SECONDS ,
                newArrayBlockingQueue<Runnable>(60) , Executors.defaultThreadFactory() , newThreadPoolExecutor.CallerRunsPolicy());
    }
    
    
    //方式二
    
    //自定义线程池
    @Autowired
    privateThreadPoolPropertiesthreadPoolProperties ;
​
    @Value("${spring.application.name}")
    privateStringapplicationName ;
​
    /**
     * 配置一个线程池
     */
    @Bean
    publicThreadPoolExecutorthreadPoolExecutor() {
​
        // 创建一个ThreadPoolExecutor对象
        /**
         int corePoolSize: 核心线程数
         int maximumPoolSize:最大线程数
         long keepAliveTime:临时线程最大的空闲时间
         TimeUnit unit:时间单位
         BlockingQueue<Runnable> workQueue:任务队列
         ThreadFactory:线程工厂
         RejectedExecutionHandler handler:任务的拒绝策略
         */
        ThreadPoolExecutorthreadPoolExecutor=newThreadPoolExecutor(threadPoolProperties.getCorePoolSize()
                , threadPoolProperties.getMaximumPoolSize(),
                threadPoolProperties.getKeepAliveTime(), TimeUnit.MINUTES, newArrayBlockingQueue<>(threadPoolProperties.getWorkQueueSize()),
                newThreadFactory() {
                    intnum=1 ;       // 计数器,用来标识线程名称
                    @Override
                    publicThreadnewThread(Runnabler) {
                        Threadthread=newThread(r);
                        thread.setName("thread-【"+applicationName+"】-"+num++);
                        returnthread;
                    }
​
                }, newThreadPoolExecutor.AbortPolicy()) ;
​
        returnthreadPoolExecutor ;
    }
​
}
2、线程池参数通过配置文件进行配置
@Data
@ConfigurationProperties(value="app.threadpool")
publicclassThreadPoolProperties {
​
    intcorePoolSize=4;
    intmaximumPoolSize=8;
    longkeepAliveTime=1000*60*5L;      // 以毫秒为单位
    intworkQueueSize=1000;                 // 压测来决定
​
}

使用ThreadPoolProperties对ThreadPoolConfiguration配置类进行改造。

3、自定义注解对@Import线程池配置类进行封装
@Target(value=ElementType.TYPE)
@Retention(value=RetentionPolicy.RUNTIME)
@Import(value=ThreadPoolConfiguration.class)
public @interface EnableThreadPool {
​
}
​
​

//如果需要使用线程池只需要给启动类加上这个自定义注解就可以了

4、在微服务中启用线程池功能,并且加入线程池配置,编写测试类进行测试

2.1也可以在别的模块自定义配置

1.application.yml加入如下配置

# 线程池参数配置
app:
  threadpool:
    corePoolSize: 4
    maximumPoolSize: 10
    keepAliveTime: 300
    workQueueSize: 60
  1. 启动类加上之前的自定义注解

@EnableThreadPool
测试类:
@SpringBootTest(classes=ItemApplication.class)
publicclassThreadPoolTest {
​
    @Autowired
    privateThreadPoolExecutorthreadPoolExecutor ;
​
    @Test
    publicvoidtestThreadPoolExecutor() {
        threadPoolExecutor.execute(() -> {
            System.out.println(Thread.currentThread().getName() +"----->执行了任务");
        });
    }
​
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值