I have a question about how Thread pools work in servers.
Assume that I am using a multithreaded server which allocates a new thread to each incoming user request.
I have deployed Java code to search for a word in a big book the user provides. Let us say search for the word 'best' in the novel 'War and Peace'.
I use FixedThreadPool with size 5, so 5 threads will search various parts of the book simultaneously.
Does this mean that each user gets 5 threads? Or do all users get a total of 5 threads?
If there are 20 users sending a request simultaneously, will there be 100 threads or 5 threads shared by 20 users?
It depends on your implementation: Do you only have one ThreadPool that is accessed by all connections - then all connections will only share this one ThreadPool. But if you create a new ThreadPool per connection then each connection gets its own ThreadPool - resulting in NumberOfConnections x SizeOfThreadPool Threads total.
Matthew Bendford wrote:It depends on your implementation: Do you only have one ThreadPool that is accessed by all connections - then all connections will only share this one ThreadPool. But if you create a new ThreadPool per connection then each connection gets its own ThreadPool - resulting in NumberOfConnections x SizeOfThreadPool Threads total.
Thankyou Matthew, I am deploying a SpringBoot application which uses an embedded Tomcat server. So there must be a default. Is the default - one ThreadPool per connection if I use ExecutorService + FixedThreadPool + SpringBoot + (Embedded) Tomcat. Do you know?
Webapps are not continuously-executing programs, they're called by the webapp container when an incoming HTTP URL request matches the context path assigned to that webapp, at which point the webapp server pulls a thread from the worker thread pool, determines which servlet/JSP to execute, and sends the thread into it.
If you need to run a long-running task in a web application, you cannot use these threads for that. Nor - per the JEE spec - are you allowed to spawn a child thread to do that. If you do, you can train-wreck the entire server, including unrelated webapps.
Servlets and JSPs are expected to run as quickly as possible and return a result ASAP. You have to be more creative if you need something that takes time.
The usual approach to that is to set up some services in a ServletContextListener's "begin" method. This is code that will be executed under one of Tomcat's main threads and doesn't have the constraints that worker threads do. You can, indeed, setup your own private thread pool (or a single thread), a work queue (thread-safe!) and an "engine" method that can be called from the servlets/jsps to add work to the queue, query progress, test for work completed, and so forth. When the webapp terminates, the "end" ServletContextlistener method then terminates and cleans up all the engine threads (a JVM won't shut down if there are live threads anywhere in it).
Bjoke: A "Bully Joke". A Statement or action made with malicious intent - unless challenged. At which point it magically transforms into "I was just funnin'" or "What's the matter, can't take a joke?"