How to use Async in Spring Boot?
Below is my code.
With my below code, different thread ids are not getting created.
The output has same thread id.
@Controller
@RequestMapping(value = "/Main")
public class MyController
{
@Autowired
private MyService myService;
@PostMapping("/Sub")
@ResponseBody
public String readInput(@RequestBody String name)
{
for (int i = 0;i<5;i++)
{
myService.asyncMethod();
}
return "Success";
}
}
With my below code, different thread ids are not getting created.
@Repository
@Configuration
@EnableAsync
public class MyService {
@Bean(name = "threadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
@Async("threadPoolTaskExecutor")
public void asyncMethod() {
System.out.println("Thread " + Thread.currentThread().getId()+ " is running");
}
}
from Recent Questions - Stack Overflow https://ift.tt/3FSxSqM
https://ift.tt/eA8V8J
Comments
Post a Comment