Java - SpringBoot异步线程池 ThreadPoolTaskExecutor
2021/5/21 20:25:24
本文主要是介绍Java - SpringBoot异步线程池 ThreadPoolTaskExecutor,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、配置线程池
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; @Configuration public class ExecutorConfig { @Bean public Executor asyncServiceExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5);// 配置核心线程数 executor.setMaxPoolSize(10);// 配置最大线程数 executor.setQueueCapacity(100);// 配置队列大小 executor.setThreadNamePrefix("async-service-");// 配置线程池中的线程的名称前缀 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 设置拒绝策略 executor.initialize();// 执行初始化 return executor; } }
*ThreadPoolExecutor的拒绝策略: 当线程池已经达到maxPoolSize的时候, 如何处理新任务.
- AbortPolicy: 丢弃任务并抛出RejectedExecutionException异常
- DiscardPolicy: 丢弃任务, 但是不抛出异常
- DiscardOldestPolicy: 丢弃队列最前面的任务, 然后重新提交被拒绝的任务
- CallerRunsPolicy: 由调用线程(提交任务的线程)处理该任务
二、构建异步线程业务层
*AsyncService
import java.util.concurrent.Future; public interface AsyncService { Future<String> getResult(); void doSomething(); }
*AsyncServiceImpl
import com.lky.service.AsyncService; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Component; import java.util.concurrent.Future; @Component @Async public class AsyncServiceImpl implements AsyncService { @Override public Future<String> getResult() { // 业务处理代码... return new AsyncResult<>("result"); } @Override public void doSomething() { // 业务处理代码... System.out.println("do something"); } }
- @Async注解, 用在类上表示该类所有方法均为异步, 用在方法上表示仅该方法为异步.
三、调用异步线程
import com.lky.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @Controller @EnableAsync public class AsyncController { @Autowired private AsyncService asyncService; @RequestMapping("/async") @ResponseBody public Map<String, String> async(@RequestBody Map<String, String> params) throws ExecutionException, InterruptedException { Future<String> asyncResult = asyncService.getResult(); System.out.println("asyncResult = " + asyncResult.get()); return null; } }
- 若异步线程任务有返回值, 当使用get方法获取返回值时会造成线程阻塞, 从而导致异步变同步.
- @EnableAsync注解, 若当前类需要使用异步线程任务, 必须在类上使用该注解.
这篇关于Java - SpringBoot异步线程池 ThreadPoolTaskExecutor的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-07如何利用看板工具优化品牌内容创作与审批,确保按时发布?
- 2025-01-07百万架构师第十一课:源码分析:Spring 源码分析:Spring源码分析前篇|JavaGuide
- 2025-01-07质量检测标准严苛,这 6 款办公软件达标了吗?
- 2025-01-07提升品牌活动管理的效率:看板工具助力品牌活动日历的可视化管理
- 2025-01-07宠物商场的精准营销秘籍:揭秘看板软件的力量
- 2025-01-07“30了,资深骑手” | 程序员能有什么好出路?
- 2025-01-07宠物公园的营销秘籍:看板软件如何帮你精准触达目标客户?
- 2025-01-07从任务分解到资源优化:甘特图工具全解析
- 2025-01-07企业升级必备指南:从传统办公软件到SaaS工具的转型攻略
- 2025-01-07一文告诉你IT项目管理如何做到高效