我有一个 spring boot 应用程序,想通过使用 @async 注释运行一个简单的异步后台任务,但是,这似乎不起作用(没有创建新线程,我等到每次调用函数后睡眠结束。 . *****我的配置类:**
@Configuration
@EnableAsync
public class AsyncConfig {
}
Run Code Online (Sandbox Code Playgroud)
****我的控制器:**
@GetMapping(value = "/testAsync")
@ResponseBody
public String testAsync() {
TestClass testClass = new TestClass();
System.out.println("begin of test async dates is : " + new Date());
for (int i = 0; i < 5; i++) {
testClass.asyncMethod();
}
System.out.println("end of test async dates is : " + new Date());
return "end!";
}
Run Code Online (Sandbox Code Playgroud)
*****我的TestClass其中的异步方法:**
@Service
public class TestClass {
@Async
public void asyncMethod() {
System.out.println("Execute method asynchronously. …Run Code Online (Sandbox Code Playgroud)