• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How do threads work in servers

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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?

Thanks.
 
Master Rancher
Posts: 180
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Divya Dutta
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Saloon Keeper
Posts: 24499
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomcat has a worker thread pool.

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).
reply
    Bookmark Topic Watch Topic
  • New Topic