SpringBoot 使用异步线程池方式如下
第一种
- 创建自定义线程池配置类,
AsyncTaskExecutePool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| @EnableAsync @Configuration public class AsyncTaskExecutePool {
private final int corePoolSize = 10; private final int maxPoolSize = 15; private final int queueCapacity = 50; private final int keepAliveSeconds = 60;
@Bean public Executor myAsyncTaskPool() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setKeepAliveSeconds(keepAliveSeconds); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setThreadNamePrefix("my-async1--"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; } }
|
- 创建任务处理类
AsyncTask
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Component @Slf4j public class AsyncTask {
@Async("myAsyncTaskPool") public void run(int i){ log.info("我是:" + i); } }
|
- 测试线程池
AppTests
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @SpringBootTest class AppTests {
@Autowired private AsyncTask asyncTask;
@Test void test(){ for (int i = 0; i < 100; i++) { asyncTask.run(i); } } }
|
运行查看效果
第二种
第二种方式是重写 spring
默认线程池,使用这种方式的好处是可以直接使用 @Async
注解
- 创建配置类
AsyncTaskExecutePool1
并且实现AsyncConfigurer
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| @Slf4j @EnableAsync @Configuration public class AsyncTaskExecutePool1 implements AsyncConfigurer {
private final int corePoolSize = 10; private final int maxPoolSize = 15; private final int queueCapacity = 50; private final int keepAliveSeconds = 60;
@Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setKeepAliveSeconds(keepAliveSeconds); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setThreadNamePrefix("my-async-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; }
@Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return (throwable, method, objects) -> { log.error("===="+throwable.getMessage()+"====", throwable); log.error("exception method:"+method.getName()); }; } }
|
- 修改
AsyncTask
类,在类中加入方法run1
1 2 3 4
| @Async public void run1(int i){ log.info("我是:" + i); }
|
- 测试,在
AppTests
中加入方法test1
1 2 3 4 5 6
| @Test void test1(){ for (int i = 0; i < 100; i++) { asyncTask.run1(i); } }
|
运行查看效果
注意
- 同类中调用带有
@Async
的方法是不生效的
- 例子中的参数根据具体的需求修改
源码
本文源码:https://github.com/elunez/spring-boot-learn