Yes the standard JDK ThreadPoolExecutor behavior is bad. Here is a good thread describing how to fix ThreadPoolExecutor to behave how it should be.
https://stackoverflow.com/questions/19528304/how-to-get-the-threadpoolexecutor-to-increase-threads-to-max-before-queueing Obviously Tomcat had to do similar thing. I wish that JDK would add some kind of easy option to ThreadPoolExecutor to add more threads instead of adding to queue, when all existing threads are busy, because the current behavior does not make sense. -Harri -----Original Message----- From: john.e.gr...@wellsfargo.com.INVALID <john.e.gr...@wellsfargo.com.INVALID> Sent: tiistai 12. maaliskuuta 2024 18.54 To: users@tomcat.apache.org Subject: RE: When does Tomcat add and remove threads? [Et saa yleensä sähköpostia john.e.gr...@wellsfargo.com.invalid. Lisätietoja siitä, miksi tämä on tärkeää, on osoitteessa https://aka.ms/LearnAboutSenderIdentification ] All, > -----Original Message----- > From: Christopher Schultz <ch...@christopherschultz.net> > Sent: Tuesday, March 12, 2024 8:31 AM > To: users@tomcat.apache.org > Subject: Re: When does Tomcat add and remove threads? > > John, > > On 3/11/24 18:14, john.e.gr...@wellsfargo.com.INVALID wrote: > > From: Christopher Schultz <ch...@christopherschultz.net> > > Sent: Monday, March 11, 2024 5:09 PM > > > >> On 3/11/24 17:47, john.e.gr...@wellsfargo.com.INVALID wrote: > >>> I am using Tomcat 9.x. > >>> > >>> When does Tomcat add and remove threads from its internal thread > >>> pool? I'm talking about the threads with names like > >>> http-nio-8080-exec-1. It appears the thread pool is Tomcat's own > >>> ThreadPoolExecutor but I don't see the exact behavior documented. > >>> I'm familiar with how java.util.concurrent does it, but it looks > >>> like Tomcat's version is a little different. > >> > >> Are you looking for a technical explanation with code references, > >> or a plain-English description of when threads are created and > >> added? > > > Mostly plain English like the j.u.c. ThreadPoolExecutor Java doc has. > > What happens when all core threads are in use? When do tasks go on > > the queue? When does core thread + 1 get added? When do threads > > get removed? > Tomcat will create thread pools under two separate circumstances. They > are related, but behave somewhat differently. > > First, if you declare an <Executor> in your server.xml, then a thread > pool will be created. You can control the number of threads and their > retention policy such as "keep X spare threads around" and "retire > threads after N seconds without being used." > > Second, if you declare a <Connector> without specifying an "executor", > a thread pool will be configured for you but you don't really have > control over it because all those nice configuration options for an > <Executor> are not available on the <Connector>. If you want to > control those settings, use a <Connector> linked with an <Executor>. > To be clear, if you declare a <Connector> without an "executor" > attribute, your thread pool will be of a fixed size and threads will > never be released. (I think the thread pool starts small and grows but > will never shrink.) > > An <Executor> is implemented in the StandardThreadExecutor and > ThreadPoolExecutor classes, which I believe were adaptations of > classes from java.util.concurrent introduced into Tomcat before > java.util.concurrent was actually available -- which is why it wasn't > used directly in Tomcat. (NB: The ThreadPoolExecutor class in Tomcat > contains an "@author Doug Lea" tag. The Tomcat source is licensed > under AL2, the JDK source is licensed under GPL2, but the original was > released by Doug Lea into the public domain under a CC0 > 1.0 Deed license.) > > The re-sizing occurs in the ThreadPoolExecutor class if you'd like to > read it. It is not entirely straightforward. You could start by > reading the code for the runWorker(Worker w) method where, at the end, > processWorkerExit is called. > > But since Tomcat's ThreadPoolExecutor is basically Java's > ThreadPoolExecutor, they work the same. > > -chris > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org I took matters in to my own hands and wrote a test to see what was happening. Unlike java.util.concurrent.ThreadPoolExecutor, Tomcat's TPE adds threads when all core threads are busy. It only adds tasks to the queue when max threads are busy. I prefer this behavior to the JDK behavior. Threads are removed when idle for 60 seconds. This appears to be hard-coded in AbstreactEndpoint.createExecutor(). Thanks B KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKCB [ X ܚX KK[XZ[ \ \ ][ X ܚX P X ] \X K ܙ B ܈Y][ۘ[ [X[ K[XZ[ \ \ Z[ X ] \X K ܙ B