I want to write a Spring Boot application with non-blocking requests in mind. In general, my controller methods are single-line methods, which just call a service method and then return, like this:
@RestController
class myController {
...
@GetMapping("/")
public String getString(){
return service.getString().get();
}
}
Let's say I have a Service implementation which looks something like this:
@Service
class myService{
...
@Async
public CompleteableFuture<String> getString(){
return "Hello World!";
}
}
(Assume I have elsewhere included @EnableAsync, and assume the controller code is modified to handle CompleteableFutures by extracting the String value and returning it)
When my controller calls my service, does Java still reserve a worker thread to wait on the return value from the Service, or is the worker thread returned to the thread pool until the service completes its work?
Thanks.
When my controller calls my service, does Java still reserve a worker thread to wait on the return value from the Service
No, your controller does not wait, the method @Async method is called asynchronously.
or is the worker thread returned to the thread pool until the service completes its work?
By default when @Async method is called Spring uses SimpleAsyncTaskExecutor which starts a new thread for each invocation, so no thread pool is used. You can however provide your own Task Executor or use ThreadPoolTaskExecutor - details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With