一、简单介绍

方式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的更多相关文章

  1. [开源项目]可观测、易使用的SpringBoot线程池

    在开发spring boot应用服务的时候,难免会使用到异步任务及线程池.spring boot的线程池是可以自定义的,所以我们经常会在项目里面看到类似于下面这样的代码 @Bean public Ex ...

  2. SpringBoot线程池和Java线程池的实现原理

    使用默认的线程池 方式一:通过@Async注解调用 public class AsyncTest { @Async public void async(String name) throws Inte ...

  3. springboot 线程池

    我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...

  4. springboot线程池的使用和扩展(转)

    springboot线程池的使用和扩展 我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行, ...

  5. springboot线程池的使用和扩展

    我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...

  6. springboot线程池@Async的使用和扩展

    我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...

  7. 【Java分享客栈】SpringBoot线程池参数搜一堆资料还是不会配,我花一天测试换你此生明白。

    一.前言   首先说一句,如果比较忙顺路点进来的,可以先收藏,有时间或用到了再看也行:   我相信很多人会有一个困惑,这个困惑和我之前一样,就是线程池这个玩意儿,感觉很高大上,用起来很fashion, ...

  8. SpringBoot线程池的创建、@Async配置步骤及注意事项

    最近在做订单模块,用户购买服务类产品之后,需要进行预约,预约成功之后分别给商家和用户发送提醒短信.考虑发短信耗时的情况所以我想用异步的方法去执行,于是就在网上看见了Spring的@Async了. 但是 ...

  9. springboot线程池任务调度类 -- ThreadPoolTaskScheduler介绍

    springboot中有一个bean,ThreadPoolTaskScheduler,可以很方便的对重复执行的任务进行调度管理:相比于通过java自带的周期性任务线程池ScheduleThreadPo ...

  10. Springboot 线程池配置

    最近的项目里要手动维护线程池,然后看到一起开发的小伙伴直接用Java了,我坚信Springboot不可能没这功能,于是查了些资料,果然有,这里给一下. 首先我们都知道@Async标签能让方法异步执行, ...

随机推荐

  1. 【结对作业】第一周 | 学习体会day02

    今天我们想要实现线路的查询 发现了几个错误 1 <%-- 下拉表单的命名使用错误,导致无法接收前端数值--%> 首先我们很少使用下拉表单,之前用的也忘了,然后格式出现了错误 2 遇到typ ...

  2. 自学 --day9---js中的数学操作和时间日期

    typora-copy-images-to: media 一.数学处理 1.Math常用API 圆周率 Math.PI // 3.1415926535 生成随机数 Math.random() 生成的是 ...

  3. 我用 AI 写的《JavaScript 工程师的 Python 指南》电子书发布啦!

    关于本书 你好,我是 luckrnx09,一名靠 React 恰饭的前端工程师,很高兴向你介绍我的第一本开源电子书<JavaScript 工程师的 Python 指南>. 本书的内容完全免 ...

  4. Kubernetes: client-go 源码剖析(一)

    0. 前言 在看 kube-scheduler 组件的过程中遇到了 kube-scheduler 对于 client-go 的调用,泛泛的理解调用过程总有种隔靴搔痒的感觉,于是调转头先把 client ...

  5. 【UniApp】-uni-app-处理项目输入数据(苹果计算器)

    前言 上一篇文章完成了项目的基本布局,这一篇文章我们来处理一下项目的输入数据 项目的输入数据主要是通过按键来输入的,所以我们需要对按键进行处理 那么我们就来看一下 uni-app-处理项目输入数据 步 ...

  6. 华企盾DSC邮件服务器测试连接提示Bad login or password(账号密码错误)

    解决方法:出现该提示说明账号和密码有一个填错了,注意:这里的密码不是邮箱本身的密码,是授权码,具体可以在邮箱设置中查看,而且必须开启smtp服务才能正常使用.

  7. 【C#】【命名空间(namespace)】.NET6.0后支持的顶级语句使用问题

    创建C#项目且使用.Net6.0以上的版本时,默认code会使用顶级语句形式: 1.略去static void Main(String[ ] args)主方法入口: 2.隐式使用(即隐藏且根据代码所需 ...

  8. CVE-2016-5734 复现

    CVE-2016-5734 漏洞简介 phpMyAdmin 4.0.x-4.6.2 远程代码执行漏洞(CVE-2016-5734) phpMyAdmin是一套开源的.基于Web的MySQL数据库管理工 ...

  9. Python——第五章:随机模块random

    1.浮点数random.random() 的返回值是在 [0, 1)(左闭右开区间)内的随机浮点数.这意味着它可以取到 0,但不包括 1.所以,random.random() 可以返回 0,但不能返回 ...

  10. 构建健康游戏环境:DFA算法在敏感词过滤的应用

    现在的游戏有敏感词检测这一点,相信大家也不陌生了,不管是聊天,起名,签名还是简介,只要是能让玩家手动输入的地方,一定少不了敏感词识别,至于识别之后是拒绝修改还是星号替换,这个就各有各的做法了,但是绕不 ...