首页 > 解决方案 > Spring Async 问题:仅使用 Autowired 实例创建的新线程,而不是创建的“新”线程

问题描述

尝试了异步演示项目:https ://github.com/huanzi-qch/springBoot/tree/master/springboot-async 。它对我来说很好。但事实证明,如果实例是由“new”关键字创建的,带有“Async”注解的方法将在同一个线程中执行!!这种不同行为的原因是什么?

@Autowired
private TestService testService;
...
System.out.println(Thread.currentThread().getName() + ":开始调用异步业务");
//无返回值
//TestService testService = new TestServiceImpl();  // If uncomment this line, no new thread created
testService.asyncTask();

标签: javaspring-bootasynchronous

解决方案


推荐阅读