tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin Gainty" <>
Subject Re: Is ThreadLocal safe to use in servlets
Date Sun, 24 Jun 2007 15:18:26 GMT
Just so I're advocating ThreadLocal over immutable variables 
or Local variables?

This email message and any files transmitted with it contain confidential
information intended only for the person(s) to whom this email message is
addressed.  If you have received this email message in error, please notify
the sender immediately by telephone or email and destroy the original
message without making a copy.  Thank you.

----- Original Message ----- 
From: "Caldarale, Charles R" <>
To: "Tomcat Users List" <>
Sent: Saturday, June 23, 2007 4:34 PM
Subject: RE: Is ThreadLocal safe to use in servlets

> From: lightbulb432 []
> Subject: Re: Is ThreadLocal safe to use in servlets
> A different way of asking this question is where/when
> does Tomcat start the Thread that handles a given
> request, and what is executed in the context of that
> thread? (e.g. filters, listeners, other things, servlets
> of course)

Tomcat utilizes a thread pool for processing requests; the same thread
is used for the duration of the request through the entire
filter/servlet chain (required by the servlet spec), including data base
and other external resource access.  However, since the threads are
pooled, you can't count on ever seeing the same thread twice in a given
servlet, so ThreadLocal objects are best created and destroyed during
individual request handling; trying to keep them around longer runs the
risk of their continuing to exist even after a webapp has been stopped
or undeployed, resulting in garbage collection problems.

Since Listeners are not associated with a specific request, there's no
guarantee that the thread running through any Listener code will ever be
used for a request for the associated webapp, and there's no guarantee
it won't.

 - Chuck

MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message