最新SpringBoot2使用线程池-超简单详解

首先配置我们自己的线程池

  1. 创建配置文件ThreadPoolConfigProperties

    @ConfigurationProperties(prefix = "my.thread")
    @Component
    @Data
    public class ThreadPoolConfigProperties {
    
        private Integer coreSize;// 线程池核心线程数
        private Integer maxSize;// 最大线程数
        private Integer keepAliveTime;// 当线程数大于核心时,这是多余的空闲线程在终止前等待新任务的最长时间
        
    // 这里还有很多配置
        
    // corePoolSize - 保留在池中的线​​程数,即使它们是空闲的,除非设置allowCoreThreadTimeOut
    // maximumPoolSize – 池中允许的最大线程数
    // keepAliveTime – 当线程数大于核心时,这是多余的空闲线程在终止前等待新任务的最长时间。
    // unit – keepAliveTime参数的时间单位
    // workQueue – 用于在执行任务之前保存任务的队列。此队列将仅保存由execute方法提交的Runnable任务。
    // threadFactory – 执行器创建新线程时使用的工厂
    // handler – 由于达到线程边界和队列容量而阻塞执行时使用的处理程序
    }
    
  2. application.properties

    my.thread.coreSize=12
    my.thread.maxSize=120
    my.thread.keepAliveTime=10
    
  3. 创建线程池配置MyThreadConfig

    @Configuration
    public class MyThreadConfig {
    
        @Bean
        public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
            return new ThreadPoolExecutor(
                    pool.getCoreSize(),
                    pool.getMaxSize(),
                    pool.getKeepAliveTime(),
                    TimeUnit.SECONDS,// keepAliveTime参数的时间单位
                    new LinkedBlockingDeque<>(100000),// 用于在执行任务之前保存任务的队列。此队列将仅保存由execute方法提交的Runnable任务
                    Executors.defaultThreadFactory(),// 执行器创建新线程时使用的工厂
                    new ThreadPoolExecutor.AbortPolicy());//由于达到线程边界和队列容量而阻塞执行时使用的处理程序
        }
    }
    

然后就可以使用了

@Service
public class MyThreadPoolServiceImpl implements MyThreadPoolService {
    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public String testThreadPool() throws ExecutionException, InterruptedException {

        CompletableFuture<String> supplyAsync1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync1当前正在执行的线程:" + Thread.currentThread().getId());
            String s = String.valueOf(Thread.currentThread().getId());
            return s;
        }, executor);
        CompletableFuture<String> supplyAsync2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync2当前正在执行的线程:" + Thread.currentThread().getId());
            String s = String.valueOf(Thread.currentThread().getId());
            return s;
        }, executor);
        CompletableFuture<String> supplyAsync3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync3当前正在执行的线程:" + Thread.currentThread().getId());
            String s = String.valueOf(Thread.currentThread().getId());
            return s;
        }, executor);
        CompletableFuture<String> supplyAsync4 = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync4当前正在执行的线程:" + Thread.currentThread().getId());
            String s = String.valueOf(Thread.currentThread().getId());
            return s;
        }, executor);

        String s1 = supplyAsync1.get();
        String s2 = supplyAsync2.get();
        String s3 = supplyAsync3.get();
        String s4 = supplyAsync4.get();

        return s1+s2+s3+s4;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值