springboot:嵌套使用异步注解@Async还会异步执行吗

本文探讨了在SpringBoot中,当一个使用@Async注解的异步方法内部调用另一个同样异步的方法时,是否会保持异步执行。通过代码示例展示了在类级别使用@Async使得所有方法异步,然后分析了嵌套调用导致的执行时间问题,揭示了这可能由于异步执行的原理——代理机制造成。结论是避免在异步方法内部调用其他异步方法以确保预期的异步行为。
摘要由CSDN通过智能技术生成

一、引言

在前边的文章《[springboot:使用异步注解@Async的那些坑》中介绍了使用@Async注解获取任务执行结果的错误用法,今天来分享下另外一种常见的错误。

二、代码演示

下面是我的controller的代码,

package com.atssg.controller;

import com.atssg.service.MyAsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/sync/")
@RestController
public class SyncController2 {
    @Autowired
    private MyAsyncService syncService;

    @GetMapping("/test2")
    public String test2() {
        return syncService.syncMethod("hello world");
    }
}

在controller中调用了service层的syncMethod方法,

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值