admin管理员组

文章数量:1122826

I have built a SpringBoot application with REST APIs, One of these multithreaded APIs is implemented using executor service.

ExecutorService service = Executors.newFixedThreadPool(threadPoolSize);

I have built a SpringBoot application with REST APIs, One of these multithreaded APIs is implemented using executor service.

ExecutorService service = Executors.newFixedThreadPool(threadPoolSize);

Now my question is that if a thread is created to handle a request, when this thread gets free, can it be used to handle the multithreading tasks (handled by executor service), aka can these two share same thread pools. There is no other configuration done wrt executor service, or tomcat.

Also if you guys can link some articles or other resources to better understand multithreading in springboot and java, it would be of great help. I am new to these concepts.

Share Improve this question edited Nov 25, 2024 at 21:28 Mohamed amine ben hassen 8701 gold badge11 silver badges29 bronze badges asked Nov 21, 2024 at 19:04 Jahnvi TanwarJahnvi Tanwar 234 bronze badges 3
  • This won't be a good idea because one request could block all other requests from being handled when it consumes enough of the other http acceptor threads - a classic DoS. – SpaceTrucker Commented Nov 21, 2024 at 20:50
  • Not clear what you're asking. Is it that if a thread created outside of the executor can still execute tasks submitted to the executor? in that case, no. – forty-two Commented Nov 21, 2024 at 22:34
  • For more clarification my question is: are the threads created by tomcat to handle API requests and the threads handled by executor service, come from the same thread pool, or two separate thread pools are maintained in Java ? – Jahnvi Tanwar Commented Dec 1, 2024 at 16:32
Add a comment  | 

1 Answer 1

Reset to default 0

I think that is not good idea for handle request by using Executor service. Because Embedded tomcat which is included spring-boot-starter-web dependency can handle thread pool itself. You can just configure on application properties / yaml file.

If you want to use multiple thread, use or test another use cases for example processing huge file (at least more than 2GB).

For handle requests asynchronously, I will recommend spring-boot-flux project.

本文标签: