How to configure thread pool in springboot project

1. Add the following configuration information to the application.properties file.
Project structure:
Insert picture description here

#配置自定义的线程池
gulimall.thread.core-size=20
gulimall.thread.max-size=200
gulimall.thread.keep-alive-time=10

Two, create an attribute binding class

//跟配置文件绑定
@ConfigurationProperties(prefix = "gulimall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
    
    
    private Integer coreSize;
    private Integer maxSize;
    private Integer keepAliveTime;
}

3. Create a configuration class and pass the value in the attribute to the thread pool

//开启属性配置(因为ThreadPoolConfigProperties写了@Component,已经注入到容器中了,所以就不用写了)
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {
    
    
    //ThreadPoolConfigProperties是从spring容器中取出来的值
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
    
    
        return new ThreadPoolExecutor(pool.getCoreSize(),
                pool.getMaxSize(),
                pool.getKeepAliveTime(), TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}

Because @Component has been used in ThreadPoolConfigProperties to hand over objects to the spring container for management. Then in the method here, the bound value can be obtained directly from the spring container.
Practical examples:

@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    
    
    //注入线程池
    @Autowired
    ThreadPoolExecutor executor;
    /**
     * 本方法需要异步编排处理
     * 1 2 没关系 查谁都行
     * 3 4 5 必须依赖 1(1 -> 3、4、5)
     * 3 4 5 是并列的,得等 1 完成
     * 2 又和 1 没关系
     */
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
    
    

        //要返回给前端的大对象
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
    
    
            //1 sku基本信息获取 pms_sku_info
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);

        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((result) -> {
    
    
            //3 spu的销售属性组合
            Long spuId = result.getSpuId();
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(spuId);
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((result) -> {
    
    
            //4 spu的介绍 pms_spu_info_desc
            Long spuId = result.getSpuId();
            SpuInfoDescEntity desc = spuInfoDescService.getById(spuId);
            skuItemVo.setDesc(desc);
        }, executor);

        CompletableFuture<Void> baseFuture = infoFuture.thenAcceptAsync((result) -> {
    
    
            //5 spu的规格参数信息
            Long spuId = result.getSpuId();
            Long catalogId = result.getCatalogId();
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);


        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
    
    
            //2 sku图片信息 pms_sku_images
            List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);

        //异步编排 - 阻塞等结果
        //等待所有1 2 3 4 5任务都完成 infoFuture可以省略
        CompletableFuture.allOf(saleAttrFuture, descFuture, baseFuture, imagesFuture).get();

        return skuItemVo;
    }
}

Guess you like

Origin blog.csdn.net/u014496893/article/details/113915170