一、简单介绍

方式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. PTA数组及排序查找题解与解题思路

    PTA数组及排序查找题解与解题思路 函数题目 函数题目为平台提供的裁判程序调用所完成的函数进行判题,题目规定语言为C语言 6-1 求出二维数组的最大元素及其所在的坐标 本题较为简单,考察的是如何遍历一 ...

  2. jenkins安装部署、主从架构、slave镜像、K8S对接

    介绍 CI/CD工具,自动化持续集成和持续部署,用于构建各种自动化任务. 官方提供了docker镜像https://hub.docker.com/r/jenkins/jenkins 使用Deploym ...

  3. Spring Boot 2.x 到 3.2 的全面升级指南

    Spring Framework 是一种流行的开源企业级框架,用于创建在 Java Virtual Machine (JVM) 上运行的独立.生产级应用程序.而Spring Boot 是一个工具,可以 ...

  4. Windows 激活系统提示0x80072F8F错误代码的解决方法(刷新你的认知)

    Server2008.Server2012.Server2016.Win7.Win10都适用 https://blog.csdn.net/happyxjbf/article/details/10591 ...

  5. wasm+pygbag让你在网页上也能运行Python代码:【贪吃蛇游戏】

    引言 最近小伙伴告诉我一种新的方法,可以使用wasm来使浏览器网页能够运行Python代码.这一下子激起了我的兴趣,因为这意味着用户无需安装Python环境就能直接运行我的demo,这真是太方便了.所 ...

  6. DVWA CSRF:Cross-site request forgery(跨站请求伪造)全等级

    CSRF:Cross-site request forgery(跨站请求伪造) 目录: CSRF:Cross-site request forgery(跨站请求伪造) 1.Low 2.Medium 3 ...

  7. springmvc内嵌tomcat、tomcat整合springmvc、自研国产web中间件

    springmvc内嵌tomcat.tomcat整合springmvc.自研国产web中间件 这是由于公司老项目转化springboot存在太多坑,特别是hibernate事务一条就坑到跑路,你又不想 ...

  8. 使用Spring AI让你的Spring Boot应用快速拥有生成式AI能力

    之前分享了关于Spring新项目Spring AI的介绍视频.视频里演示了关于使用Spring AI将Open AI的能力整合到Spring应用中的操作,但有不少读者提到是否有博客形式的学习内容.所以 ...

  9. AES加密技术:原理与应用

    一.引言 随着信息技术的飞速发展,数据安全已成为越来越受到重视的领域.加密技术作为保障数据安全的重要手段,在信息安全领域发挥着举足轻重的作用.AES(Advanced Encryption Stand ...

  10. JS模块化编程规范1——require.js

    目录 1. 概述 2. 详论 2.1. 定义 2.2. 调用 2.3. 入口 3. 结果 4. 参考 1. 概述 require.js是各种网络APP中非常常见的JS依赖库,它其实不仅仅是个模块加载器 ...