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

Springboot结合线程池的使用

1.使用配置文件配置线程的参数

配置文件

thread-pool:core-size: 100max-size: 100keep-alive-seconds: 60queue-capacity: 1

配置类

@Component
@ConfigurationProperties("thread-pool")
@Data
public class ThreadPoolConfig {private int coreSize;private int maxSize;private int keepAliveSeconds;private int queueCapacity;
}

2.配置线程池并使用

方式一:线程池结合CompletableFuture来实现

配置线程池类

@Configuration
public class ThreadPoolTask1 {@Autowiredprivate ThreadPoolConfig threadPoolConfig;@Bean("task1")public ThreadPoolTaskExecutor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(threadPoolConfig.getCoreSize()); // 核心线程数executor.setMaxPoolSize(threadPoolConfig.getMaxSize()); // 最大线程数executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds()); // 非核心线程活跃时间executor.setQueueCapacity(threadPoolConfig.getQueueCapacity()); // 队列容量executor.setThreadNamePrefix("test-"); // 设置线程的前缀名executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 设置拒绝策略executor.setWaitForTasksToCompleteOnShutdown(false); // 是否在任务执行完后关闭线程池executor.initialize();return executor;}
}

CompletableFuture使用线程池进行调用

package com.example.demo;import com.example.demo.config.ThreadPoolTask1;
import com.sun.java.browser.plugin2.DOM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.stream.Collectors;@RestController
public class Demo {@Autowiredprivate DemoServiceImpl demoService;@Autowired@Qualifier("task1")private ThreadPoolTaskExecutor threadPoolTask1;@GetMapping("thread1")public Map<String, String> thread1() {long start = System.currentTimeMillis();List<CompletableFuture<String>> futures = new ArrayList<>();Map<String, String> map = new HashMap<>();// 使用CompletableFuture的 supplyAsync来处理结果相当于submit, runAsync无返回相当于executefor (int i = 0; i < 100; i++) {int b = i;CompletableFuture<String> future = CompletableFuture.supplyAsync(() ->demoService.executorTask1(b), threadPoolTask1);futures.add(future);}// 获取结果集CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).whenComplete((v, th) ->futures.stream().forEach(item -> {String result = null;try {result = item.get();} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}map.put(result.split("-")[1], result.split("-")[0]);})).join();long end = System.currentTimeMillis();map.put("当前时间",(end- start) + "");return map;}
}

任务类

    public String executorTask1(int i) {System.out.println("当前线程-" + Thread.currentThread().getName());try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 6) {throw new RuntimeException("cs");} else {return "aaa-" + i;}}

方式二:使用@EnableAsync和@Async方式实现

在启动类上加@EnableAsync注解

// 加上@EnableAsync注解,也可以在自己的配置类上加
@EnableAsync // 开启对异步任务的支持
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

编写线程池配置

// 也可以在此处加上@EnableAsync注解,入口类上不加
@Configuration
public class ThradPoolTask{@Autowiredprivate ThreadPoolConfig threadPoolConfig;@Beanpublic Executor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(threadPoolConfig.getCoreSize()); // 核心线程数executor.setMaxPoolSize(threadPoolConfig.getMaxSize()); // 最大线程数executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds()); // 非核心线程活跃时间executor.setQueueCapacity(threadPoolConfig.getQueueCapacity()); // 队列容量executor.setThreadNamePrefix("test-"); // 设置线程的前缀名executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 设置拒绝策略executor.setWaitForTasksToCompleteOnShutdown(false); // 是否在任务执行完后关闭线程池executor.initialize();return executor;}
}

使用

@RestController
public class Demo {@Autowiredprivate DemoServiceImpl demoService;@GetMapping("thread")public Map<String, String> thread() {long start = System.currentTimeMillis();Map<String, String> map = new HashMap<>();List<Future<String>> futures = new ArrayList<>();for (int i = 0; i < 100; i++) {Future<String> future = demoService.executorTask(i);futures.add(future);}int b = 1;futures.forEach(item -> {try {String result = item.get();map.put(result.split("-")[1], result.split("-")[0]);} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}});long end = System.currentTimeMillis();map.put("当前时间",(end- start) + "");return map;}
}

任务类

    // 必须指明使用的是哪个线程池,taskExecutor不带的话用springboot默认注册的线程池// Future作为返回值,携带返回结果@Async("taskExecutor")public Future<String> executorTask(int i) {System.out.println("当前线程-" + Thread.currentThread().getName());try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 6) {throw new RuntimeException("cs");} else {// AsyncResult返回携带的结果return new AsyncResult<String>("aaa-" + i);}}

方式三:重写springboot默认的线程池配置

在启动类上加@EnableAsync注解

// 加上@EnableAsync注解,也可以在自己的配置类上加
@EnableAsync
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
@Configuration
public class ThreadPoolTask2 implements AsyncConfigurer {@Autowiredprivate ThreadPoolConfig threadPoolConfig;/*** 修改默认线程池的配置** @return*/@Overridepublic Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(threadPoolConfig.getCoreSize()); // 核心线程数executor.setMaxPoolSize(threadPoolConfig.getMaxSize()); // 最大线程数executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds()); // 非核心线程活跃时间executor.setQueueCapacity(threadPoolConfig.getQueueCapacity()); // 队列容量executor.setThreadNamePrefix("test2-"); // 设置线程的前缀名executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 设置拒绝策略executor.setWaitForTasksToCompleteOnShutdown(false); // 是否在任务执行完后关闭线程池executor.initialize();return executor;}/*** 修改默认的异常处理* 注意:如果带有返回值Future,异常会被捕获,不会去执行该方法** @return*/@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {return (ex, method, params) -> {System.out.println("任务执行的异常" + ex);System.out.println("执行的任务方法" + method.getName());for (Object param : params) {System.out.println("执执行任务的参数" + param);}};}
}

使用


@RestController
public class Demo {@Autowiredprivate DemoServiceImpl demoService;// 不带返回值的任务@GetMapping("thread2")public Map<String, String> thread2() {long start = System.currentTimeMillis();Map<String, String> map = new HashMap<>();List<Future<String>> futures = new ArrayList<>();for (int i = 0; i < 100; i++) {demoService.executorTask2(i);}return null;}// 带有返回值的任务@GetMapping("thread3")public Map<String, String> thread3() {long start = System.currentTimeMillis();Map<String, String> map = new HashMap<>();List<Future<String>> futures = new ArrayList<>();for (int i = 0; i < 100; i++) {Future<String> future = demoService.executorTask3(i);futures.add(future);}int b = 1;futures.forEach(item -> {String result = null;try {result = item.get();} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}map.put(result.split("-")[1], result.split("-")[0]);});long end = System.currentTimeMillis();map.put("当前时间",(end- start) + "");return map;}
}

任务类

	// @Async不需要指定,使用默认即可// 出现异常会走AsyncUncaughtExceptionHandler方法@Asyncpublic void executorTask2(int i) {System.out.println("当前线程-" + Thread.currentThread().getName());try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}int a = 1/0;}// 即使出现异常也不会走AsyncUncaughtExceptionHandler方法@Asyncpublic Future<String> executorTask3(int i) {System.out.println("当前线程-" + Thread.currentThread().getName());try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 6) {throw new RuntimeException("cs");} else {return new AsyncResult<String>("aaa-" + i);}}

关于三种在执行过程中的异常

方式一:导致请求失败:最好在任务中进行处理
在这里插入图片描述

方式二:请求成功了,关于6的那条数据并没有返回给前端
在这里插入图片描述

方式三:请求成功了,关于6的那条数据并没有返回给前端

相关文章:

Springboot结合线程池的使用

1.使用配置文件配置线程的参数 配置文件 thread-pool:core-size: 100max-size: 100keep-alive-seconds: 60queue-capacity: 1配置类 Component ConfigurationProperties("thread-pool") Data public class ThreadPoolConfig {private int coreSize;private int ma…...

AOP工作流程

AOP工作流程3&#xff0c;AOP工作流程3.1 AOP工作流程流程1:Spring容器启动流程2:读取所有切面配置中的切入点流程3:初始化bean流程4:获取bean执行方法验证容器中是否为代理对象验证思路步骤1:修改App类,获取类的类型步骤2:修改MyAdvice类&#xff0c;不增强步骤3:运行程序步骤…...

Modbus相关知识点及问题总结

本人水平有限&#xff0c;写得不对的地方望指正 困惑&#xff1a;线圈状态的值是否是存储在线圈寄存器里面&#xff1f;是否有线圈寄存器的说法&#xff1f;网上有说法说是寄存器占两个字节&#xff0c;但线圈的最少操作单位是位。类似于继电器的通断状态&#xff0c;直接根据电…...

【MySQL】函数

文章目录1. DQL执行顺序2. 函数2.1 字符串函数2.2 数值函数2.3 日期函数2.4 流程函数2.5 窗口函数2.5.1 介绍2.5.2 聚合窗口函数2.5.3 排名窗口函数2.5.4 取值窗口函数1. DQL执行顺序 2. 函数 2.1 字符串函数 函数功能concat(s1,s2,…sn)字符串拼接&#xff0c;将s1,s2…sn拼…...

MySQL高级

一、基础环境搭建 环境准备&#xff1a;CentOS7.6&#xff08;系统内核要求是3.10以上的&#xff09;、FinalShell 1. 安装Docker 帮助文档 : https://docs.docker.com/ 1、查看系统内核&#xff08;系统内核要求是3.10以上的&#xff09; uname -r2、如果之前安装过旧版本的D…...

带你弄明白c++的4种类型转换

目录 C语言中的类型转换 C强制类型转换 static_cast reinterpret_cast const_cast dynamic_cast RTTI 常见面试题 这篇博客主要是帮助大家了解和学会使用C中规定的四种类型转换。首先我们先回顾一下C语言中的类型转换。 C语言中的类型转换 在C语言中&#xff0c;如果赋…...

8个明显可以提升数据处理效率的 Python 神库

在进行数据科学时&#xff0c;可能会浪费大量时间编码并等待计算机运行某些东西。所以我选择了一些 Python 库&#xff0c;可以帮助你节省宝贵的时间 文章目录1、Optuna技术提升2、ITMO\_FS3、Shap-hypetune4、PyCaret5、floWeaver6、Gradio7、Terality8、Torch-Handle1、Optun…...

互联网公司吐槽养不起程序员,IT岗位的工资真是虚高有泡沫了?

说实话&#xff0c;看到这个话题的时候又被震惊到。 因为相比以往&#xff0c;程序员工资近年来已经够被压缩的了好嘛&#xff1f; 那些鼓吹泡沫论的&#xff0c;真就“何不食肉糜”了~~~ 而且这种逻辑就很奇怪&#xff0c; 程序员的薪资难道不是由行业水平决定么&#xff…...

Excel 进阶|只会 Excel 也能轻松搭建指标应用啦

现在&#xff0c;Kyligence Zen 用户可在 Excel 中对指标进行更进一步的探索和分析&#xff0c;能够实现对维度进行标签筛选、对维度基于指标值进行筛选和排序、下钻/上卷、多样化的透视表布局、本地 Excel 和云端 Excel 的双向支持等。业务人员和分析师基于现有分析习惯就可以…...

RabbitMQ中TTL

目录一、TTL1.控制后台演示消息过期2.代码实现2.1 队列统一过期2.2 消息过期一、TTL TTL 全称 Time To Live&#xff08;存活时间/过期时间&#xff09;。 当消息到达存活时间后&#xff0c;还没有被消费&#xff0c;会被自动清除。 RabbitMQ可以对消息设置过期时间&#xff0…...

Ceres简介及示例(4)Curve Fitting(曲线拟合)

文章目录1、Curve Fitting1.1、残差定义1.2、 Problem问题构造1.3、完整代码1.4、运行结果2、Robust Curve Fitting1、Curve Fitting 到目前为止&#xff0c;我们看到的示例都是没有数据的简单优化问题。最小二乘和非线性最小二乘分析的原始目的是对数据进行曲线拟合。 以一个…...

音质最好的骨传导蓝牙耳机有哪些,推荐几款不错的骨传导耳机

​骨传导耳机也称为“不入耳式”耳机&#xff0c;是一种通过颅骨、骨迷路、内耳淋巴液和听神经之间的信号传导&#xff0c;来达到听力保护目的的一种技术。由于它可以开放双耳&#xff0c;所以在跑步、骑行等运动时使用十分安全&#xff0c;可以避免外界的干扰。这种耳机在佩戴…...

计算机操作系统安全

操作系统安全是计算机系统安全的重要组成部分&#xff0c;目的是保护操作系统的机密性、完整性和可用性。在当前的网络环境下&#xff0c;操作系统面临着许多威胁&#xff0c;如病毒、木马、蠕虫、黑客攻击等等。为了保护操作系统的安全&#xff0c;需要采取各种措施来防范这些…...

超详细从入门到精通,pytest自动化测试框架实战教程-用例标记/执行(三)

目录&#xff1a;导读前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09;前言 pytest可以通过标记…...

Java SE 基础(5) Java 环境的搭建

Java 虚拟机——JVM JVM &#xff08;Java Virtual Machine &#xff09;&#xff1a;Java虚拟机&#xff0c;简称JVM&#xff0c;是运行所有Java程序的假想计算机&#xff0c;是Java程序的运行环境&#xff0c;是Java 最具吸引力的特性之一。我们编写的Java代码&#xff0c;都…...

银行数字化转型导师坚鹏:银行对公客户数字化场景营销案例萃取

银行对公客户数字化场景营销案例萃取与行动落地课程背景&#xff1a; 很多银行存在以下问题&#xff1a;不清楚银行数字化营销与场景营销内涵&#xff1f;不知道如何开展对公客户数字化营销工作&#xff1f;不知道对公业务数字化场景营销成功案例&#xff1f; 学员收获&a…...

get和post的区别

1.用途上 get请求用来向服务器获取资源&#xff1b; post请求用来向服务器提交数据&#xff1b; 2.表单提交方式上 get请求直接将表单数据拼接到URL上&#xff0c;多个参数之间通过&符号连接&#xff1b; post请求将表单数据放到请求头或者请求体中&#xff1b; 3.传…...

Java调用Oracle存储过程

文章目录 Java调用Oracle存储过程Java调用Oracle存储过程 使用Java实现存储过程的步骤: 1、数据表、存储过程【已完成】 2、引入依赖包、数据源配置 3、Java实现【已完成】 – Oracle 创建数据表 CREATE TABLE STUDENT ( ID NUMBER (20) NOT NULL ENABLE PRIMARY KEY, NAME V…...

ubuntu如何设置qt环境变量

Qt 是一个1991年由Qt Company开发的跨平台C图形用户界面应用程序开发框架。它既可以开发GUI程序&#xff0c;也可用于开发非GUI程序&#xff0c;比如控制台工具和服务器。Qt是面向对象的框架&#xff0c;使用特殊的代码生成扩展&#xff08;称为元对象编译器(Meta Object Compi…...

高管对谈|揭秘 NFT 技术背后的研发方法论

有人说&#xff0c;元宇宙是未来&#xff0c;NFT 则是通往这个可能的未来的数字通行证。 经过一度热炒之后&#xff0c;NFT 逐渐回归理性的「大浪淘沙」轨迹。NXTF_&#xff08;廿四未来&#xff09;正是一家将 NFT 向实体经济靠拢并与之结合的公司。 NXTF_利用区块链技术&am…...

是面试官放水,还是企业实在是缺人?这都没挂,字节原来这么容易进...

“字节是大企业&#xff0c;是不是很难进去啊&#xff1f;”“在字节做软件测试&#xff0c;能得到很好的发展吗&#xff1f;一进去就有9.5K&#xff0c;其实也没有想的那么难”直到现在&#xff0c;心情都还是无比激动&#xff01; 本人211非科班&#xff0c;之前在字节和腾讯…...

JVM 本地方法栈

本地方法栈的作用 Java虚拟机栈于管理Java方法的调用&#xff0c;而本地方法栈用于管理本地方法的调用。本地方法栈&#xff0c;也是线程私有的。允许被实现成固定或者是可动态扩展的内存大小&#xff08;在内存溢出方面和虚拟机栈相同&#xff09; 如果线程请求分配的栈容量超…...

GPT-4老板:AI可能会杀死人类,已经出现我们无法解释的推理能力

来源: 量子位 微信号&#xff1a;QbitAI “AI确实可能杀死人类。” 这话并非危言耸听&#xff0c;而是OpenAI CEO奥特曼的最新观点。 而这番观点&#xff0c;是奥特曼在与MIT研究科学家Lex Fridman长达2小时的对话中透露。 不仅如此&#xff0c;奥特曼谈及了近期围绕ChatGPT…...

弹性盒布局

系列文章目录 前端系列文章——传送门 CSS系列文章——传送门 文章目录系列文章目录弹性盒模型&#xff08;FlexibleBox 或 flexbox&#xff09;什么是弹性盒&#xff1f;基本配置项给父元素添加给子元素添加弹性盒案例滚动条青蛙网页练习旧的弹性盒display:box 属性浏览器的兼…...

第13章_事务基础知识

第13章_事务基础知识 &#x1f3e0;个人主页&#xff1a;shark-Gao &#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是shark-Gao&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f389;目前状况&#xff1a;23届毕业生&#xff0c;目前…...

LeetCode笔记:Biweekly Contest 101

LeetCode笔记&#xff1a;Biweekly Contest 101 1. 题目一 1. 解题思路2. 代码实现 2. 题目二 1. 解题思路2. 代码实现 3. 题目三 1. 解题思路2. 代码实现 4. 题目四 1. 解题思路2. 代码实现 比赛链接&#xff1a;https://leetcode.com/contest/biweekly-contest-101/ 1. 题…...

new和malloc两个函数详细实现与原理分析

1.申请的内存所在位置 new操作符从自由存储区&#xff08;free store&#xff09;上为对象动态分配内存空间&#xff0c;而malloc函数从堆上动态分配内存。自由存储区是C基于new操作符的一个抽象概念&#xff0c;凡是通过new操作符进行内存申请&#xff0c;该内存即为自由存储…...

[ROC-RK3568-PC] [Firefly-Android] 10min带你了解LCD的使用

&#x1f347; 博主主页&#xff1a; 【Systemcall小酒屋】&#x1f347; 博主追寻&#xff1a;热衷于用简单的案例讲述复杂的技术&#xff0c;“假传万卷书&#xff0c;真传一案例”&#xff0c;这是林群院士说过的一句话&#xff0c;另外“成就是最好的老师”&#xff0c;技术…...

【redis】redis分布式锁

目录一、为什么需要分布式锁二、分布式锁的实现方案三、redis分布式锁3.1 简单实现3.2 成熟的实现一、为什么需要分布式锁 1.在java单机服务中&#xff0c;jvm内部有一个全局的锁监视器&#xff0c;只有一个线程能获取到锁&#xff0c;可以实现线程之间的互斥 2.当有多个java服…...

UEditorPlus v3.0.0 接口请求头参数,插入换行优化,若干问题优化

UEditor是由百度开发的所见即所得的开源富文本编辑器&#xff0c;基于MIT开源协议&#xff0c;该富文本编辑器帮助不少网站开发者解决富文本编辑器的难点。 UEditorPlus 是有 ModStart 团队基于 UEditor 二次开发的富文本编辑器&#xff0c;主要做了样式的定制&#xff0c;更符…...