[infinispan-dev] Clean ThreadLocals

David M. Lloyd david.lloyd at redhat.com
Wed Dec 11 17:52:41 EST 2013


On 12/11/2013 04:47 PM, Pedro Ruivo wrote:
> Hi,
>
> I've created a method to clean a specific ThreadLocal variable from all
> live threads [1].
>
> My goal is to clean the ThreadLocal variables after a cache stops. It's
> kind expensive method (it uses reflection) but I think it is fine.
>
> Currently, I'm using it to clear the Marshaller ThreadLocal in here [2].
>
> Does anyone see any problem with this approach? Maybe it can be used in
> other possible leaking ThreadLocals?

It's an interesting idea (I've done something similar in the past to 
simply drop all thread locals).  I would recommend that you check that 
all the JDKs you want to support use the same technique for thread 
locals though.  Because these fields are not part of the JDK, they may 
not always exist in all environments.

Also, it is important to only ever clean the thread locals of your 
current thread, or you're inviting all kinds of bizarre concurrency 
problems (maybe even locking threads into infinite loops), especially if 
the thread is running and actively using thread locals at the time.
-- 
- DML


More information about the infinispan-dev mailing list