5

Is it important, or even possible, to control how many threads Spring uses in, for example, a web application?

Lets say I have a Spring based REST server. Should I, or is it possible to control the number of threads Spring uses to service requests? If I deploy my app on a machine with 8 cores vs 4, should I have to configure Spring to account for the difference in cores?

JeffLL
  • 1,875
  • 3
  • 19
  • 30
  • 2
    which web server are you using? – SMA Feb 09 '15 at 09:10
  • 2
    The number of threads (not including the executors explicitely or implicitely created to handle asynchronous or scheduled Spring methods) depends on the web server where the application is deployed (i.e. Tomcat, Jetty, etc.). Spring has no control over that. – JB Nizet Feb 09 '15 at 09:16
  • @almasshaikh usually Jetty – JeffLL Feb 09 '15 at 09:17
  • @JBNizet Ah, I had not thought of that. Thanks! In that case, I guess I would have to configure Jetty, Tomcat, or whatever webserver I am using to take full advantage of the server I'm hosting my app at? – JeffLL Feb 09 '15 at 09:18
  • 1
    Why? Just let the JVM do its job, and the OS ditto. You will rarely have the same number of active clients as CPUs, and the code that services them is going to be mostly network-bound, not CPU-bound. Don't worry about problems that don't exist. – user207421 Feb 09 '15 at 09:22
  • @EJP thanks for the info. Just trying to get to a much deeper level of understanding. Also, I am working on a even server that may potentially have hundreds of thousands of pixel fire receiving events(non web server), so understanding of that helps a lot. – JeffLL Feb 10 '15 at 00:04

1 Answers1

1

The number of request threads is controlled by the web container not by Spring. For example, if you are running on Tomcat, this Q&A explains how to configure the thread pool size: https://stackoverflow.com/a/7803226/139985

Should I, or is it possible to control the number of threads used to service requests?

It is possible, but you should generally shouldn't. Let the container deal with this ... unless you have good (evidence-based!) reason to think that the container is making a poor choice. The container defaults / policy should take account of the number of available cores.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks Stephen, do you know of a good article that shows how the container deals with number of cores and threads? Just trying to understand how that works so I can get a deeper level of knowledge. – JeffLL Feb 10 '15 at 00:03