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

spring boot 异步线程池的使用

  1. 创建Spring Boot项目
    首先,你需要创建一个Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。

  2. 添加异步支持依赖
    在你的pom.xml文件中,确保你已经添加了Spring Boot的starter依赖,特别是spring-boot-starter-web,这将提供异步支持所需的依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 配置异步线程池参数
    在Spring Boot中,你可以通过application.properties或application.yml文件来配置异步线程池的参数。以下是一个使用application.properties文件的示例:
# 异步线程池配置
async.executor.thread.core_pool_size=5
async.executor.thread.max_pool_size=10
async.executor.thread.queue_capacity=25
async.executor.thread.name.prefix=async-
async.executor.thread.keep_alive_seconds=60
async.executor.thread.await_termination_seconds=60
  1. 编写配置类以定义线程池
    接下来,你需要编写一个配置类,使用@Configuration注解来定义线程池。在这个配置类中,你将使用@Bean注解来创建一个ThreadPoolTaskExecutor实例,并根据application.properties中的配置来设置线程池的参数。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;@Configuration
@EnableAsync
public class AsyncConfig {@Value("${async.executor.thread.core_pool_size}")private int corePoolSize;@Value("${async.executor.thread.max_pool_size}")private int maxPoolSize;@Value("${async.executor.thread.queue_capacity}")private int queueCapacity;@Value("${async.executor.thread.name.prefix}")private String namePrefix;@Value("${async.executor.thread.keep_alive_seconds}")private int keepAliveSeconds;@Value("${async.executor.thread.await_termination_seconds}")private int awaitTerminationSeconds;@Bean(name = "taskExecutor")public Executor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(corePoolSize);executor.setMaxPoolSize(maxPoolSize);executor.setQueueCapacity(queueCapacity);executor.setThreadNamePrefix(namePrefix);executor.setKeepAliveSeconds(keepAliveSeconds);executor.setAwaitTerminationSeconds(awaitTerminationSeconds);executor.setWaitForTasksToCompleteOnShutdown(true);executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());executor.initialize();return executor;}
}
  1. 编写异步任务类
    现在,你可以编写一个包含异步方法的类。在这个类中,你将使用@Async注解来标记需要异步执行的方法,并指定前面定义的线程池(在这个例子中是taskExecutor)。
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class AsyncService {// 在需要使用异步处理的方法上添加注解@Async("taskExecutor")public void executeAsyncTask() {System.out.println("执行异步任务 - " + Thread.currentThread().getName());try {// 模拟长时间运行的任务Thread.sleep(5000);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}
}
  1. 最后,在你的控制器或其他服务类中,你可以调用这个异步方法来执行异步任务。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class AsyncController {@Autowiredprivate AsyncService asyncService;@GetMapping("/async")public String handleAsyncRequest() {asyncService.executeAsyncTask();return "异步任务已启动";}
}

通过以上步骤,你就成功地在Spring Boot中配置了异步线程池,并编写了一个异步任务类来演示如何使用它。这样,你就可以在需要时轻松地将任务交给线程池异步执行了。

注意:
@Async注解不生效
‌从Spring容器管理的bean中调用‌:@Async注解的方法必须是从Spring容器管理的bean中调用的。如果从一个非Spring管理的类中调用,那么异步效果将不会生效。
‌自调用问题‌:同一个类中的方法调用不会被Spring的AOP代理捕获,因此如果在一个类中调用同一个类的另一个@Async方法,那么异步效果也不会生效。为了避免这个问题,可以将异步方法移动到另一个bean中,并通过依赖注入来调用。

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

相关文章:

  • 风力涡轮机缺陷检测数据集,86.6%准确识别率,11921张图片,支持yolo,PASICAL VOC XML,COCO JSON格式的标注
  • 深入浅出梯度下降与反向传播
  • 【电机控制】基于STC8H1K28的六步换向——方波驱动(硬件篇)
  • 手搓一个ChatUI需要分几步
  • 2024年年度总结
  • labelme2yolov8-seg 草稿()
  • 开源简史与概览
  • 充电桩语音提示芯片方案-支持平台自定义语音NV128H让充电更便捷
  • 277-基于八路256Ksps 24bit AD生物电震动检测FMC子卡
  • 【电路理论四】正弦电流电路
  • 【PyCharm】如何把本地整个项目同步到服务器?
  • 露营小程序搭建有哪些步骤?小程序里面可以找个露营搭子
  • SOLID-开闭原则
  • 前端经典面试合集(二)——Vue/React/Node/工程化工具/计算机网络
  • PH47代码框架 24241231 重要更新
  • Qt6之QML——作用域
  • 119.【C语言】数据结构之快速排序(调用库函数)
  • C#封送类
  • 2024年度学习总结
  • 我的博客年度之旅:感恩、成长与展望
  • undefined symbol: __nvJitLinkComplete_12_4, version libnvJitLink.so.12
  • 【OTA】论文笔记--《智能网联汽车整车OTA功能设计研究》智能网联汽车OTA系统设计分析报告
  • c#String和StringBuilder
  • 【Linux】HTTP协议
  • 计算机网络 (14)数字传输系统
  • 《向量数据库指南》——Milvus Cloud 2.5:Sparse-BM25引领全文检索新时代
  • Unity3D 网络框架设计详解
  • 网络渗透测试实验四:CTF实践
  • Wend看源码-Java-Collections 工具集学习
  • [JAVA]MyLogger