springboot隔离@Async异步任务的线程池
Posted 灰太狼_cxh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot隔离@Async异步任务的线程池相关的知识,希望对你有一定的参考价值。
springboot隔离@Async异步任务的线程池
1.代码实现:
在#yyds干货盘点# springboot配置@Async异步任务的线程池基础代码上进行修改
设置多个线程池
@EnableAsync
@Configuration
public class TaskPoolConfig
@Bean
public Executor taskExecutor1()
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(10);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("executor-1-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
@Bean
public Executor taskExecutor2()
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(10);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("executor-2-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
创建异步任务
@Async("taskExecutor1")
public CompletableFuture<String> doTaskOne(String taskNo) throws Exception
log.info("开始任务:", taskNo);
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务:,耗时: 毫秒", taskNo, end - start);
return CompletableFuture.completedFuture("任务完成");
@Async("taskExecutor2")
public CompletableFuture<String> doTaskTwo(String taskNo) throws Exception
log.info("开始任务:", taskNo);
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务:,耗时: 毫秒", taskNo, end - start);
return CompletableFuture.completedFuture("任务完成");
这里@Async注解中定义的taskExecutor1和taskExecutor2就是线程池的名字。由于在第一步中没有具体写两个线程池Bean的名称,所以默认会使用方法名,也就是taskExecutor1和taskExecutor2。
测试类
@Test
public void testTask() throws Exception
long start = System.currentTimeMillis();
// 线程池1
CompletableFuture<String> task1 = asyncTasks.doTaskOne("1");
CompletableFuture<String> task2 = asyncTasks.doTaskOne("2");
CompletableFuture<String> task3 = asyncTasks.doTaskOne("3");
// 线程池2
CompletableFuture<String> task4 = asyncTasks.doTaskTwo("4");
CompletableFuture<String> task5 = asyncTasks.doTaskTwo("5");
CompletableFuture<String> task6 = asyncTasks.doTaskTwo("6");
// 一起执行
CompletableFuture.allOf(task1, task2, task3, task4, task5, task6).join();
long end = System.currentTimeMillis();
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
2.实现效果:
2021-12-29 17:22:37.812 INFO 18352 --- [ main] c.c.a.AsyncTaskApplicationTests : Started AsyncTaskApplicationTests in 1.571 seconds (JVM running for 2.951)
2021-12-29 17:22:38.121 INFO 18352 --- [ executor-2-1] com.cxh.async_task.task.AsyncTasks : 开始任务:4
2021-12-29 17:22:38.120 INFO 18352 --- [ executor-1-1] com.cxh.async_task.task.AsyncTasks : 开始任务:1
2021-12-29 17:22:38.121 INFO 18352 --- [ executor-1-2] com.cxh.async_task.task.AsyncTasks : 开始任务:2
2021-12-29 17:22:38.121 INFO 18352 --- [ executor-2-2] com.cxh.async_task.task.AsyncTasks : 开始任务:5
2021-12-29 17:22:38.825 INFO 18352 --- [ executor-1-1] com.cxh.async_task.task.AsyncTasks : 完成任务:1,耗时:691 毫秒
2021-12-29 17:22:38.826 INFO 18352 --- [ executor-1-1] com.cxh.async_task.task.AsyncTasks : 开始任务:3
2021-12-29 17:22:41.304 INFO 18352 --- [ executor-1-2] com.cxh.async_task.task.AsyncTasks : 完成任务:2,耗时:3170 毫秒
2021-12-29 17:22:43.090 INFO 18352 --- [ executor-2-1] com.cxh.async_task.task.AsyncTasks : 完成任务:4,耗时:4956 毫秒
2021-12-29 17:22:43.090 INFO 18352 --- [ executor-2-1] com.cxh.async_task.task.AsyncTasks : 开始任务:6
2021-12-29 17:22:44.236 INFO 18352 --- [ executor-2-2] com.cxh.async_task.task.AsyncTasks : 完成任务:5,耗时:6102 毫秒
2021-12-29 17:22:47.793 INFO 18352 --- [ executor-1-1] com.cxh.async_task.task.AsyncTasks : 完成任务:3,耗时:8967 毫秒
2021-12-29 17:22:51.937 INFO 18352 --- [ executor-2-1] com.cxh.async_task.task.AsyncTasks : 完成任务:6,耗时:8846 毫秒
2021-12-29 17:22:51.937 INFO 18352 --- [ main] c.c.a.AsyncTaskApplicationTests : 任务全部完成,总耗时:13837毫秒
以上是关于springboot隔离@Async异步任务的线程池的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot中有多个@Async异步任务时,记得做好线程池的隔离!
#yyds干货盘点# springboot配置@Async异步任务的线程池
springboot中@Scheduled 和@Async的使用