Tomcat allows by default max 200 threads to process requests in a Spring Boot application. Lets say within the application, during the start of the process, I create a FixedThreadpool of size say 10.
Now when I am trying to use this thing with tomcat, what would be the max total number of threads that I will be running. Will it be 200 10 or 200 * 10?
My understanding is that it will be 200 10 as at the process level only java will create the threadpool and use that threadpool whereever required.
Also will it depend on how i initiate the threadpool? For example let's say I create a bean of threadpool executor and use that across the system.
@Bean("helperExecutorService")
public ExecutorService helperExecutorService(HelperThreadPoolConfig helperThreadPoolConfig) {
return Executors.newFixedThreadPool(helperThreadPoolConfig.getThreadPoolSize());
}
Or I can create threadpool in the code flow.
public void something() {
ExecutorService service = Executors.newFixedThreadPool(summariesToAutocompute.size());
... use this threadpool in the method
}```
CodePudding user response:
For one application instance running on tomcat, tomcat will have one threadpool created when the server starts, the application will have the fixed threadpool created on startup. This is not a shared nothing cgi, it's a single long running java process that stays in memory, 200 10 is right.
If you create a pool within a method call then each method call would allocate its own pool. So if every thread in tomcats pool was concurrently making a request that called a method that created its own pool then the number of threads in use could be up to the tomcat pool size times the method pool size.
Generally I would recommend not doing this because it would seem to undercut two advantages of using a pool: 1) that you can preallocate threads so your code doesn't have to wait on thread creation, and 2) that you can put an upper bound on the maximum number of threads your program uses.