使用threadPoolExecutor线程池实现异步操作

本文介绍了如何在SpringBoot应用中配置JDK线程池,推荐使用`newThreadPoolExecutor`避免LinkedBlockingQueue导致的核心线程过载,同时展示了如何通过ThreadPoolExecutor实现异步查询功能。

JDK线程池的配置

@Configuration
public class AppConfig {
    @Bean
    public ThreadPoolExecutor businessPoolExecutor(){
        //创建线程池的方式
        //使用Executor 创建=>不推荐
        //不推荐的原因,里面使用的工作队列是LinkedBlockingQueue,
        // 里面默认的长度几乎无限大,会造成核心线程过载,非核心线程不进行加载,最后内存溢出

        //2使用 new ThreadPooleExecution对象=>推荐
       return new ThreadPoolExecutor(10,50,10, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>(100));

    }
}

调用方法实现异步查询

//注入ThreadPoolExecutor 
private final ThreadPoolExecutor threadPoolExecutor;
    public DestinationServiceImpl( ThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolExecutor = threadPoolExecutor;
    }
threadPoolExecutor.execute(() ->{
                //将他们的id作为父id进行查询,使用last方法进行分页查询
                List<Destination> children = list(new QueryWrapper<Destination>().in("id", 1).last("limit 10"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值