20

I understand Java NIO (channels, selector, ..). I would like to understand Tomcat NIO better so that I can configure thread pools of Tomcat appropriately from Spring boot.

Can someone please explain what is the purpose of each thread pool and how do these work in relevance to Java NIO? It would be helpful if you can also point out which thread pool is used during the processing of HTTP requests.

Some Tomcat8 thread pools observed during thread dumps:

http-nio-<port>-Acceptor (usually 1 or 2 threads)
http-nio-<port>-ClientPoller-<index> (usually 2)
http-nio-<port>-exec-<index> (usually 10)
NioBlockingSelector.BlockPoller-<index> (usually 2)
Laurel
  • 5,965
  • 14
  • 31
  • 57
Rag
  • 1,363
  • 4
  • 19
  • 36

2 Answers2

6

http-nio--exec- (usually 10) => This can be controlled by setting "server.tomcat.max-threads=10" in application.properties. If its set to 1, then you see only one thread http-nio--exec-1.

I am too trying to find out other thread pools.

Subra M
  • 581
  • 1
  • 8
  • 16
  • 1
    i think it depends on the version of tomcat used. In tomcat 8 the property is : server.tomcat.maxThreads=. – gstanley Mar 27 '19 at 15:42
  • Spring Boot allows both the camelcase `maxThreads` and the hyphenated version `max-threads`. This applies to all properties. – Piotr P. Karwasz Nov 04 '21 at 12:30
6

The proper solution with Spring and Tomcat would be to use 2 properties:

server.tomcat.max-threads=200 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=10 # Minimum amount of worker threads

If you change the server.tomcat.max-thread below the server.tomcat.min-spare-threads, then you will have as many thread as the max-thread property.

If you change the server.tomcat.min-spare-threads, then you will have as many thread as specified.

For instance, if you set to this: server.tomcat.min-spare-threads=15, then you will have 15 http-nio-8080-exec-*

RUARO Thibault
  • 2,672
  • 1
  • 9
  • 14
  • By saying "If you change the `server.tomcat.max-thread` below the `server.tomcat.min-spare-threads`, then you will have as many thread as the max-thread property." you actually mean values of `max-thread` and `min-spare-threads` properties to be EQUAL, right? – NikolaS Sep 09 '20 at 14:30
  • No, I meant below. In this case, you don’t get an error, but the max number of thread is the min spare – RUARO Thibault Sep 09 '20 at 18:18
  • In that case, be clear, e.g. if `max-thread=5` and `min-spare-threads=10` how many MAXIMUM threads will be used than in ThreadPool? Will then `max-thread` value be be **min** and **max** number of threads in ThreadPool? Thank you in advance. – NikolaS Sep 10 '20 at 08:03
  • Then you will have 5 threads. Sorry, my last comment is unclear (even wrong...) – RUARO Thibault Sep 11 '20 at 09:05
  • 1
    Doesn't answer the question at all. – Martin Andersson Jan 12 '22 at 08:13