tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Question on Thread Local
Date Tue, 02 Sep 2014 20:48:33 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Leo,

On 9/2/14, 4:22 PM, Leo Donahue wrote:
> On Tue, Sep 2, 2014 at 3:00 PM, Leon Rosenberg
> <rosenberg.leon@gmail.com> wrote:
> 
>> From practical point of view ThreadLocal is a huge hashmap
>> directly in the ThreadClass where you can store a map of
>> variables. Something like Thread.Map<ThreadId, Map<String,
>> Object>>, in which you can access variables that are 'attached'
>> logically to the current Thread. In practice its a nice way to
>> pass information through layers of code without adding it
>> explicitly as parameter to every function on the way. regards 
>> Leon
>> 
> 
> At some point in the web application, a ThreadLocal is instantiated
> and its properties are set and then retrieved in a Filter.  Am I on
> track here?

If I were to use a ThreadLocal in a web application (which I would
usually recommend against doing), I'd set the value in the Filter and
use it in other places during request processing. Then when the Filter
regains control after the chain.doFilter() call, I'd remove the
ThreadLocal from the thread using ThreadLocal.remove().

If you don't call ThreadLocal.remove() at the end of request
processing, you risk a memory leak because the ThreadLocal variable
likely contains an instance of a class loaded by your webapp's
ClassLoader.

You can certainly use objects from the standard API as ThreadLocal
values to avoid such a risk.

> How is that different or more helpful than instantiating any other
> POJO with property setters?

The only reason is that you wouldn't have to pass the object around
through all your methods to make it available to some code buried
beneath a call stack 50 calls deep.

> A POJO will be instantiated on every servlet request whereas the 
> ThreadLocal is only created once?

If you call "new PoJo()" in your Filter, then yes it will happen for
every call. If you use a ThreadLocal, the target object is only
instantiated once.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJUBi0eAAoJEBzwKT+lPKRY9HQP/RdMBcLzVn/yV+6WuV/j43g6
VZ+YflL17ipz5Xtj4tkDx2XReUAix2r0/noWqEJXEhHugCPmOVYCgeNNWc47W4Kr
Tw97xq1KJ2Bthu88IASX9MBBhiIj/A8SugLhZaCpem88d1U18nlcJdIVVCOkqDgD
yi2fEd/C6cWNvaXOgfxycH0eMI6OIGkRLJUfdvJfFTOQNBnn+f+kZL22pWdSRab5
ar8nx7MVs8a2qObFbYj1Qtxd+vuBDQzdYACDTVncnMtBdrFYeADLvXyEodGlNs6Z
Yc5E60st2M+gtPMY/8H3L7T4XHStQu6WDMTjTPDjG/zBtScJunZIWzKheWNXAE7S
8JpmjCAZrfvUz2lAOxKYeZIRl2Lq72EqSHa4w8Yb34BF6KBR+KRpBHTJnsGNNbpF
lqEAXswuU7uMpp1roDdAR6Zp6BC7hLkbOvC6XRULOSXuNUimp2whYOy8IQCDmrMo
j5zJq3NRJEqwpxUY9qBHIXaeL6JaT7eTcprmCs8fZvZKu7Kvps8TgPjvANfMOqro
AVHkSZnjI9B3aqfdjZaJ/2QiFG61cwGncGyd2VRrBN9Qf7CzWu25z427sHAZQMkW
+DQikpgOimfeju6GNB1MtXUyTt73WcXQKrWvNQRDivoNVWuXqh0i4HMslGEdrF8B
Hzy2dwMxf0Fwp9+1muZn
=zj7P
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message