springboot线程池的使用方式2
一、简单介绍
方式1:Executors.newCachedThreadPool线程池。Executors有7种不同的线程池。
private static final ExecutorService executorService = Executors.newCachedThreadPool(new BasicThreadFactory.Builder()
.namingPattern("create-card-thread-%d")
.build()); CompletableFuture.runAsync(createCards(reqVO, buId), executorService)
.whenComplete((Void v, Throwable t) -> {
if (t == null) {
log.info("create card complete.batchId={}", reqVO.getBatchId());
} else {
log.error("create card failed.batchId={}", reqVO.getBatchId(), t);
}
try (ShardingCtx s = ShardingCtx.setShardingValue(buId)) {
CustomerIntGencardLogPO updateLog = new CustomerIntGencardLogPO();
updateLog.setPk(gencardLogPO.getPk())
.setLastUpdated(LocalDateUtil.localDateTimeMinus8Hours(LocalDateTime.now()))
.setGencardStatus(PROCESSED);
customerIntGencardLogMapper.updateById(updateLog);
}
});
方式二:自定义线程池
注入:@Autowire @Resource
@Service
public class AsyncService { @Autowired //1.注入自定义的线程池 Resource 重命名
@Resource(description = "taskExecutorTest")
private ThreadPoolTaskExecutor threadPoolTaskExecutor; //自定义线程池
private static final ThreadPoolExecutor taskExecutor =
new ThreadPoolExecutor(10, 20, 20, TimeUnit.SECONDS,
new LinkedBlockingDeque<Runnable>(20), new ThreadPoolExecutor.CallerRunsPolicy()); public void addEventLog(String buId, String status) {
CustomerEventLogPO customerEventLog = new CustomerEventLogPO();
customerEventLog.setStatus(status);
customerEventLog.setCreated(LocalDateTime.now());
customerEventLogMapper.insert(customerEventLog); //submit有返回值, jdk8新写法
threadPoolTaskExecutor.submit(new Thread(() -> {
customerEventLogMapper.insert(customerEventLog);
})); //execute无返回值
threadPoolTaskExecutor.execute(new Thread(() -> {
customerEventLogMapper.insert(customerEventLog);
})); //老的写法
taskExecutor.execute(new Runnable() {
@Override public void run() {
try {
studentscount = coursesService.getStudentCount(pd);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
方式三:
springboot自带的异步线程池,在要异步的方法上直接加注解:
@Async("taskExecutor") taskExecutor是自定义的bean或者默认的。
@Async("taskExecutor")
@DynamicDatasource
public void addEventLog(String buId, String uuid, String evenType, String eventData, Exception e, String status){
CustomerEventLogPO customerEventLog = new CustomerEventLogPO();
customerEventLog.setUuid(uuid);
customerEventLog.setEventType(evenType);
customerEventLog.setEventData(eventData);
customerEventLog.setStatus(status);
customerEventLog.setDescripe(e != null ? e.getMessage() : "");
customerEventLog.setCreated(LocalDateTime.now());
customerEventLogMapper.insert(customerEventLog);
}
方式四:
springboot线程池的使用方式2的更多相关文章
- [开源项目]可观测、易使用的SpringBoot线程池
在开发spring boot应用服务的时候,难免会使用到异步任务及线程池.spring boot的线程池是可以自定义的,所以我们经常会在项目里面看到类似于下面这样的代码 @Bean public Ex ...
- SpringBoot线程池和Java线程池的实现原理
使用默认的线程池 方式一:通过@Async注解调用 public class AsyncTest { @Async public void async(String name) throws Inte ...
- springboot 线程池
我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...
- springboot线程池的使用和扩展(转)
springboot线程池的使用和扩展 我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行, ...
- springboot线程池的使用和扩展
我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...
- springboot线程池@Async的使用和扩展
我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...
- 【Java分享客栈】SpringBoot线程池参数搜一堆资料还是不会配,我花一天测试换你此生明白。
一.前言 首先说一句,如果比较忙顺路点进来的,可以先收藏,有时间或用到了再看也行: 我相信很多人会有一个困惑,这个困惑和我之前一样,就是线程池这个玩意儿,感觉很高大上,用起来很fashion, ...
- SpringBoot线程池的创建、@Async配置步骤及注意事项
最近在做订单模块,用户购买服务类产品之后,需要进行预约,预约成功之后分别给商家和用户发送提醒短信.考虑发短信耗时的情况所以我想用异步的方法去执行,于是就在网上看见了Spring的@Async了. 但是 ...
- springboot线程池任务调度类 -- ThreadPoolTaskScheduler介绍
springboot中有一个bean,ThreadPoolTaskScheduler,可以很方便的对重复执行的任务进行调度管理:相比于通过java自带的周期性任务线程池ScheduleThreadPo ...
- Springboot 线程池配置
最近的项目里要手动维护线程池,然后看到一起开发的小伙伴直接用Java了,我坚信Springboot不可能没这功能,于是查了些资料,果然有,这里给一下. 首先我们都知道@Async标签能让方法异步执行, ...
随机推荐
- FreeSWITCH在answer前主动发dtmf
操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 NAT环境的主动外呼场景下,会遇到线路侧回铃音数据无法接收的问题,需要FreeSWITCH主动发送RTP数据,发送D ...
- 悲观锁、乐观锁、mybatis-plus实现乐观锁
悲观锁.乐观锁.mybatis-plus实现乐观锁 转载自:www.javaman.cn 1.悲观锁.乐观锁 乐观锁和悲观锁是两种用于处理并发操作的数据锁定策略.它们在处理多个事务尝试同时访问和修改同 ...
- [CF1844G] Tree Weights
题目描述 You are given a tree with $ n $ nodes labelled $ 1,2,\dots,n $ . The $ i $ -th edge connects no ...
- easycom自动导入自定义组件
使用时要先创建一个这样的结构 相当于定义一个方法,所有的页面引用就可以了
- 一招MAX降低10倍,现在它是我的了
一.背景 性能优化是一场永无止境的旅程. 到家门店系统,作为到家核心基础服务之一,门店C端接口有着调用量高,性能要求高的特点. C端服务经过演进,核心接口先查询本地缓存,如果本地缓存没有命中,再查询R ...
- Python+Selenium4自动化之JS属性
应用场景 在自动化中, 能对JS代码进行增.删.改的话,可以帮助我们解决很多问题, 如:修改<a>标签的target属性,让它不打开新的窗口(_blank),从而不用频繁使用switch_ ...
- vue 2实战系列 —— 复习Vue
复习Vue 近期需要接手 vue 2的项目,许久未写,语法有些陌生.本篇将较全面复习 vue 2. Tip: 项目是基于 ant-design-vue-pro ant-design-vue-pro 由 ...
- Redis 使用的 10 个小技巧
Redis 在当前的技术社区里是非常热门的.从来自 Antirez 一个小小的个人项目到成为内存数据存储行业的标准,Redis已经走过了很长的一段路. 随之而来的一系列最佳实践,使得大多数人可以正确地 ...
- sed 命令进阶
sed 是面向行处理的,但是有时可能希望针对多行作为一个单位进行处理,在 sed 中这也是可行的,本文将介绍如何使用 sed 来同时处理多行文本 理论基础 模式空间(Pattern Space):是一 ...
- ensp命令行大全
命令符从用户视图切换到系统视图 system–view 从系统视图切换到用户视图 quit 连入接口命令 interface IP地址 子网掩码配置命令 ip address 接口IP信息查看命令 d ...