一、简单介绍

方式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. fianl详解(适合新手)

    final 1.final是Java语言中的一个关键字 2.final表示最终的,不可变的. 3.final可以修饰变量以及方法,还有类等 4.final修饰的变量? 5.final修饰的方法? 6. ...

  2. 给 Web 前端工程师看的用 Rust 开发 wasm 组件实战

    什么是wasm组件? wasm 全称 WebAssembly,是通过虚拟机的方式,可以在服务端.客户端如浏览器等环境执行的二进制程序.他有速度快.效率高.可移植的特点. 对我们 Web 前端工程最大的 ...

  3. 用dbeaver创建一个enum类型,并讲述一部分,mysql的enum类型的知识

    写这个博客的目的就是我在网上看了半天,发现没有这方面的知识,也许是老手认为这个太简单了,不过我还是告诉新人使用dbeaver来创建一个enum类型的方法: 就是enum("a",& ...

  4. 【译】Visual Studio 17.8 中我最喜欢的特性

    对于 Visual Studio 团队来说,这是忙碌的一周,他们准备了 Ignite 和 .NET Conf,并发布了最新版本的 Visual Studio 2022,即17.8版本.有很多很酷的新功 ...

  5. 使用Py2neo更新Neo4j中节点的属性值的正确姿势

    1 def findNode(name, graph): 2 matcher = NodeMatcher(graph) 3 m = matcher.match(name = name).first() ...

  6. 不会使用 EF Core 的 Code First 模式?来看看这篇文章,手把手地教你

    EF Core Code First 是什么 Code First 是 Entity Framework Core (简称 EF Core) 的一种开发模式,它允许开发人员使用纯粹的代码来定义数据模型 ...

  7. 万界星空科技QMS质量管理系统

    QMS(Quality Management System)质量管理系统是五大基础系统之一,在工业企业中被广泛的应用,在质量策划.生产过程质量监督.体系审核和文档管理等业务上发挥着不可替代的作用. 一 ...

  8. ElasticSearch之cat transforms API

    命令样例如下: curl -X GET "https://localhost:9200/_cat/transforms?v=true&format=json" --cace ...

  9. 为什么在使用onnxruntime-gpu下却没有成功调用GPU?

    20240105,记. 最近在使用GPU对onnx模型进行加速过程中(仅针对N卡,毕竟也没有别的显卡了..),遇到了点问题:就是明明在安装了合适版本的显卡驱动和CUDA后,onnx还是不能够成功调用G ...

  10. libGDX游戏开发之字体样式(七)

    libGDX游戏开发之字体样式(七) libGDX系列,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm-国内用libgdx比较少,多数情况需要去官网和googl ...