当前位置: 首页 > news >正文

SpringBoot中使用Spring自带线程池ThreadPoolTaskExecutor与Java8CompletableFuture实现异步任务示例

场景

关于线程池的使用:

Java中ExecutorService线程池的使用(Runnable和Callable多线程实现):

Java中ExecutorService线程池的使用(Runnable和Callable多线程实现)_executorservice executorservice = executors.newfix-CSDN博客

Java中创建线程的方式以及线程池创建的方式、推荐使用ThreadPoolExecutor以及示例:

Java中创建线程的方式以及线程池创建的方式、推荐使用ThreadPoolExecutor以及示例_threadpoolexecutor创建线程-CSDN博客

项目开发中多使用SpringBoot,Spring中有个自带的线程池ThreadPoolTaskExecutor

Spring 通过任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor实现一个基于线程池的TaskExecutor

ThreadPoolTaskExecutor是spring core包中的,而ThreadPoolExecutor是JDK中的JUC。

ThreadPoolTaskExecutor是对ThreadPoolExecutor进行了封装处理。

ThreadPoolTaskExecutor这个类则是spring包下的,是spring为我们提供的线程池类。

SpringBoot默认情况下帮我们自动配置了ThreadPoolTaskExecutor到IOC容器中,我们需要的时候直接注入使用即可。

如果我们不想要SpringBoot帮我们默认配置的线程池参数,我们可以自行配置,ThreadPoolTaskExecutor支持对线程池核心参数的重新配置。

注:

博客:
霸道流氓气质-CSDN博客

实现

1、以若依项目为例

若依前后端分离版手把手教你本地搭建环境并运行项目:

若依前后端分离版手把手教你本地搭建环境并运行项目_本地运行若依前后端分离-CSDN博客

ruoyi中对Spring默认的线程池参数进行配置,配置文件位置

配置文件内容

package com.ruoyi.framework.config;import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import com.ruoyi.common.utils.Threads;/*** 线程池配置** @author ruoyi**/
@Configuration
public class ThreadPoolConfig
{// 核心线程池大小private int corePoolSize = 50;// 最大可创建的线程数private int maxPoolSize = 200;// 队列最大长度private int queueCapacity = 1000;// 线程池维护线程所允许的空闲时间private int keepAliveSeconds = 300;@Bean(name = "threadPoolTaskExecutor")public ThreadPoolTaskExecutor threadPoolTaskExecutor(){ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setMaxPoolSize(maxPoolSize);executor.setCorePoolSize(corePoolSize);executor.setQueueCapacity(queueCapacity);executor.setKeepAliveSeconds(keepAliveSeconds);// 线程池对拒绝任务(无线程可用)的处理策略executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());return executor;}
}

2、Java中使用CompletableFuture实现异步任务

Java8中CompletableFuture实现异步任务编排以及示例:

Java8中CompletableFuture实现异步任务编排以及示例_java并发 completablefuture异步编程的实现-CSDN博客

3、在需要使用线程池的地方直接注入

    @Autowiredprivate ThreadPoolTaskExecutor threadPoolTaskExecutor;

4、线程池的使用

编写单元测试并统计耗时

    @Testpublic void test2() {StopWatch stopWatch = new StopWatch();stopWatch.start();for (int i = 0; i < 5; i++) {int finalI = i;CompletableFuture.runAsync(() -> {System.out.println(finalI + "执行异步操作。。。");int result = 0;for (int j = 0; j < 1000000; j++) {result += j;}System.out.println("计算结果:"+result);}, threadPoolTaskExecutor);}stopWatch.stop();System.out.println("总耗时"+stopWatch.getLastTaskTimeMillis());}

运行结果

为形成对比,编写以下测试

    @Testpublic void test1() {StopWatch stopWatch = new StopWatch();stopWatch.start();for (int i = 0; i < 5; i++) {int result = 0;for (int j = 0; j < 1000000; j++) {result += j;}System.out.println("计算结果:"+result);}stopWatch.stop();System.out.println("总耗时"+stopWatch.getLastTaskTimeMillis());}

运行结果

http://www.lryc.cn/news/294799.html

相关文章:

  • OpenCV/C++:点线面相关计算(二)
  • 2024最新版鸿蒙HarmonyOS开发工具安装使用指南
  • Spring事务源码解析
  • 71.Spring和SpringMVC为什么需要父子容器?
  • 标准库 STM32+EC11编码器+I2C ssd1306多级菜单例程
  • 通过 ChatGPT 的 Function Call 查询数据库
  • LLM(大语言模型)——大模型简介
  • SQLserver2008 r2 下载安装配置、使用、新建登录用户及通过Navicat远程连接
  • linux code server 网页版的vscode
  • 【leetcode100-086到090】【动态规划】一维五题合集2
  • 关闭Ubuntu 默认开启的自动安全更新
  • python统计文本 2022年9月青少年电子学会等级考试 中小学生python编程等级考试二级真题答案解析
  • HomeAssistant系统添加HACS插件商店与远程控制家中智能家居
  • 计算huggingface模型占用硬盘空间的实战代码
  • Leetcode 3031. Minimum Time to Revert Word to Initial State II
  • 游戏后端如何实现服务器之间的负载均衡?
  • es6中标签模板
  • 二级C语言笔试1
  • Spring MVC跨域设置
  • 基于Python的HTTP隧道安全性分析:魔法背后的锁与钥匙
  • linux的stat/lstat函数和目录遍历函数使用
  • HTTP MIME 类型
  • Mac OS中创建适合网络备份的加密镜像文件:详细步骤与参数选择
  • Java TreeSet 添加自定义对象 必须指定排序规则
  • vue - 指令(一)
  • 正则表达式 regex
  • iOS自动打包如何用Python实现
  • springboot161基于springboot的公交线路查询系统
  • 大白话介绍循环神经网络
  • GEE——如何利用降水数据绘制指定区域长时间序列的降水分布图和提取每个月(逐月)的降水平均数据