新闻详情

新闻详情

首页 / 资讯中心 / 详情

Spring注解--@Async异步执行的方法

发布时间:2026/11/4 19:35:20来源:尧图网络
Spring注解--@Async异步执行的方法
原文网址Spring注解--Async异步执行的方法-CSDN博客简介本文介绍Spring的Async的用法。Async是用来异步执行任务的。基础代码正常情况下执行两个任务是这样的Controllerpackage com.knife.example.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; Slf4j Api(tags 测试) RequestMapping(test) RestController public class HelloController { Autowired private HelloService helloService; ApiOperation(测试1) PostMapping(test1) public void test() { helloService.task1(); helloService.task2(); } }Servicepackage com.knife.example.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; Slf4j Component public class HelloService { public void task1() { log.info(开始任务1); long start System.currentTimeMillis(); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } long end System.currentTimeMillis(); log.info(完成任务1耗时 (end - start) 毫秒); } public void task2() { log.info(开始任务2); long start System.currentTimeMillis(); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } long end System.currentTimeMillis(); log.info(完成任务2耗时 (end - start) 毫秒); } }它的结果是是顺序执行的。2024-07-07 15:53:00.067 INFO 1044 --- [nio-8080-exec-1] c.knife.example.controller.HelloService : 开始任务1 2024-07-07 15:53:03.067 INFO 1044 --- [nio-8080-exec-1] c.knife.example.controller.HelloService : 完成任务1耗时3000毫秒 2024-07-07 15:53:03.067 INFO 1044 --- [nio-8080-exec-1] c.knife.example.controller.HelloService : 开始任务2 2024-07-07 15:53:06.073 INFO 1044 --- [nio-8080-exec-1] c.knife.example.controller.HelloService : 完成任务2耗时3006毫秒使用异步加快速度假如上边task1和task2是不相关的那完全可以同时运行加快速度下边就用异步来做1.启用异步EnableAsync在启动类上加EnableAsyncpackage com.knife.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; EnableAsync SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }2.将方法标记为Async为便于维护全文已转移到此网址Spring-Async异步执行的方法 - 自学精灵
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/4 15:56:54

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/3 12:09:35

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/4 17:01:23

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/4 12:33:12

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/4 1:25:31

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/4 3:51:11

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉