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标签能让方法异步执行, ...
随机推荐
- MongoDB入门级别教程全(Windows版,保姆级教程)
下载mongodb 进入官网: Download MongoDB Community Server | MongoDB 选择msi,Windows版本 下载完后直接双击: 选择complete 这里建 ...
- raft算法的自我理解
1.raft算法是什么? 答:共识算法 2.raft算法有什么用? 答:维持不同机器的强一致性 3.raft算法通过什么方式来维持不同机器的强一致性? 答:传递log日志 ,按照官方的说法日志里面包含 ...
- [ICPC2014 WF] Pachinko
[ICPC2014 WF] Pachinko 题面翻译 题目描述 有一个宽度为 \(w\) 高度为 \(h\) 的方格纸, $ w \times h$ 的格子中,有一些是空的,有一些是洞,有一些是障碍 ...
- JOISC2020题解
\(\text{By DaiRuiChen007}\) Contest Link A. Building 4 Problem Link 题目大意 给 \(2n\) 个数对 \((a_i,b_i)\), ...
- 使用Py2neo更新Neo4j中节点的属性值的正确姿势
1 def findNode(name, graph): 2 matcher = NodeMatcher(graph) 3 m = matcher.match(name = name).first() ...
- ElasticSearch 命令执行漏洞
漏洞编号:CVE-2014-3120 漏洞详情 CVE编号 CVE-2014-3120 漏洞级别 中危6.8 标题 Elasticsearch默认配置允许动态脚本执行漏洞 披露时间 2014/07/2 ...
- 企业u盘禁止访问如何解锁
如果您遇到了U盘禁止访问的问题,可能是由于系统设置.安全策略或第三方工具导致的.以下是一些可能的解锁方法,具体的操作可能因具体情况而异: 管理员权限: 确保您有足够的管理员权限来解锁U盘.有时,系统管 ...
- Net 高级调试之十五:经典的锁故障
一.简介 今天是<Net 高级调试>的第十五篇文章,这个系列的文章也快结束了,但是我们深入学习的脚步还不能停止.上一篇文件我们介绍了C# 中一些锁的实现逻辑,并做到了眼见为实的演示给大家它 ...
- 春秋云镜 - CVE-2022-28060
Victor CMS v1.0 /includes/login.php 存在sql注入 找到页面的登录框,看介绍应该是post类型的表单注入. 上sqlmap用原本的梭发现ctf的那个表是空的,换用- ...
- 2024-01-06:用go语言,在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧 在桥上有一些石子,青蛙很讨厌踩在这些石子上 由于桥的长度和青蛙一次跳过的距离都是正整数 我们可以把独木桥
2024-01-06:用go语言,在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧 在桥上有一些石子,青蛙很讨厌踩在这些石子上 由于桥的长度和青蛙一次跳过的距离都是正整数 我们可以把独木桥 ...