spring注解@Async线程池(2)

问题

有些时候,我们需要等所有线程执行完后,再执行后面的业务代码。解决方案可以有:

方案一:使用信号量 CountDownLatch
1.自定义线程池配置(Bean定义)

@Bean("markUserThread")
public ThreadPoolTaskExecutor executeMarkUserThread() {
        ThreadPoolTaskExecutor poolExecutor = new ThreadPoolTaskExecutor();
        poolExecutor.setCorePoolSize(5);
        poolExecutor.setMaxPoolSize(60);
        poolExecutor.setQueueCapacity(5000);
        poolExecutor.setKeepAliveSeconds(10);
        poolExecutor.setThreadNamePrefix("markUserThread-");
        poolExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        poolExecutor.setAwaitTerminationSeconds(60);
        poolExecutor.initialize();
        return poolExecutor;
    }

定义了一个核心线程数为5,最大线程数60的线程池。

2.线程池Bean注入

@Resource(name = "markUserThread")
private ThreadPoolTaskExecutor poolTaskExecutor;

3.线程池使用

// 对大集合(任务)进行切分
List<List<Object>> partitionList = Lists.partition(dataList, 100);
        // 计算要运行的线程数
        int threadNum = partitionList.size();
        threadNum = Math.min(threadNum, 60);
         // 初始化信号量
        CountDownLatch latch = new CountDownLatch(threadNum);
        partitionList.forEach(list -> {
            poolTaskExecutor.submit(() ->                   
                // 业务代码
            );
            // 信号量减1
            latch.countDown();
        });
        try {
            // 等待所有线程执行完成
            latch.await();
            log.info("usePoolTaskExecutorMarkUserTag end->{}", threadNum);
        } catch (Exception e) {
            throw e;
        }

方案二:使用CompletableFuture
1.自定义线程池(同上)
2.注解@Async引入

@Async("markUserThread")
public CompletableFuture<String> dataListHandler(List<Integer> dataList) {
        dataList.forEach(rs -> {
            log.info("dataListHandler->{}", rs);
        });
        return CompletableFuture.completedFuture("success");
    }

3.业务调用(⚠️必须在其他类文件中调用加了Async注解的方法)

// 定义Future对象
List<CompletableFuture<?>> futures = Lists.newArrayList();
// 对大集合(任务)进行切分
List<List<Integer>> result = Lists.partition(elements, 100);
result.forEach(res -> {             
futures.add(asyncCommand.dataListHandler(res));
});            
// 等待所有线程执行完成
CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[0])).join();
log.info("task over!!!");
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要配置使用@Async注解线程池,你可以按照以下步骤进行操作: 1. 在你的Spring Boot应用程序的配置类中,添加@EnableAsync注解来启用异步方法的支持。 ```java @Configuration @EnableAsync public class AsyncConfig { // 配置其他的Bean或者方法 } ``` 2. 创建一个线程池的Bean,并在该Bean中进行线程池的配置。你可以使用ThreadPoolTaskExecutor类来创建一个线程池。 ```java @Configuration @EnableAsync public class AsyncConfig { @Bean(name = "threadPoolTaskExecutor") public Executor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); // 设置核心线程池大小 executor.setMaxPoolSize(20); // 设置最大线程池大小 executor.setQueueCapacity(30); // 设置队列容量 executor.setThreadNamePrefix("Async-"); // 设置线程名称前缀 executor.initialize(); return executor; } } ``` 在上面的示例中,我们创建了一个名为"threadPoolTaskExecutor"的Bean,并进行了一些常见的线程池配置,如核心线程池大小、最大线程池大小、队列容量和线程名称前缀。 3. 在你想要异步执行的方法上使用@Async注解,并指定要使用的线程池。 ```java @Service public class MyService { @Async("threadPoolTaskExecutor") public void asyncMethod() { // 异步执行的方法逻辑 } } ``` 在上面的示例中,我们在MyService类中的asyncMethod方法上使用了@Async注解,并指定了要使用的线程池为"threadPoolTaskExecutor"。 这样,当调用asyncMethod方法时,方法将在异步线程池中执行,而不是在调用线程中执行。 希望这些步骤对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值