tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: http thread shutdown question
Date Wed, 11 Nov 2015 20:54:41 GMT

On 11/11/15 2:13 PM, David E. Filip wrote:
> I have a question about the threads that Tomcat uses for servicing
> requests.  My environment is Tomcat 7.0.55 running on Linux (CentOS
> 6.5) with Oracle JVM 1.7.0_79.
> My question is specifically about the threads that Tomcat uses for
> servicing requests which are named ‘http-bio-{port}-exec-###’, as
> revealed by Thread.getName().  I have — for better or worse — written
> a servlet that caches some information using
> ThreadLocal[.set()][.get()]  for performance.  This mostly works very
> well, except that the number of threads in Tomcat’s Execution pool
> seem to change, with old threads going away, and new ones being
> created, and I have a need to know when any of these threads go away,
> so that I can perform some clean-up.

If you use ThreadLocal.set, then you need to re-set it at the end of a
request, if that ThreadLocal contains any kind of request state. So, the
right place to perform that clean-up is "at the end of the service()

> I am not asking about maxThreads, minThreads, etc., and I get how to
> configure the number of threads in the pool.  However, if I configure
> a particular pool size to say 50, I will see in the Tomcat Manager /
> Status page that the pool never goes above 50 (and current threads is
> usually much lower).  When the threads start out, they will begin
> as:
> http-bio-{port}-exec-1 http-bio-{port}-exec-2 http-bio-{port}-exec-3 
> … etc …
> However, if I leave the server running, they will eventually become:
> http-bio-{port}-exec-101 http-bio-{port}-exec-102 
> http-bio-{port}-exec-103 … etc …
> even though there are always less than 50 threads at any given time.

Correct... the thread pool manages its own thread population, sometimes
removing a thread because it's no longer necessary, and sometimes
creating a new one depending upon demand.

> So my question is whether there is any way to tell when one of the
> threads is stopped (removed from the queue?) by Tomcat, and if I can
> hook any code to execute before it does?  My goal is to be able to
> tell when a thread is about to be killed so that I can do some
> clean-up.

There is certainly no spec-compatible way to do this. There may be a way
to do it using Tomcat internals, but it's going to be (a) messy and (b)
error prone. It might be better to re-think your approach.

> Or is the answer [do not use] ThreadLocal for anything that needs 
> clean-up, as there is no way to tell when Tomcat is going to stop
> (remove?) a thread from its queue?

I would phrase it differently, but the upshot is the same.

> Extra credit question: when does Tomcat decided to kill a thread,
> since although understand that would happen when the queue size
> expands and collapses, on my development server I doubt I’m ever
> going beyond minThreads.  Does each thread only handle a fixed number
> of requests before it terminates, perhaps to prevent memory leaks?

No, Tomcat doesn't decommission threads at regular intervals just for
the fun of it. There are certain operations that can cause Tomcat to
recycle *all* of its threads. One operation in particular has to do with
a web application shutting down while leaving ThreadLocals bound to
Threads whose classes were loaded from the sunsetting
WebappClassLoader... in that case, Tomcat will recycle the whole thread
pool to prevent your old application from sticking around in memory forever.

(NB: Other than ThreadLocal perhaps, or a bug in the JVM, there is
nothing special about a thread that could (a) leak in a way that is
thread-specific or (b) be fixed by killing the thread.)

> I am hoping that this is the best place to ask this question.

This is the best place to ask this question.

> I have tried searching the ‘Net, and couldn’t find anything, although
> I acknowledge that perhaps I am asking something that is just not 
> possible.

Googling for this question is unlikely to yield much. Searching for
"tomcat thread" is only going to give you about 10 trillion hits with
everything off-topic for what you want. You came to the right place.

> Thanks in advance to anyone who can shed any light on this.  And I do
> know how to hook a servlet being shut down, which is not what I am
> asking.

Maybe it would help if you described your use-case... maybe someone has
a better idea that doesn't require ThreadLocal.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message