[concurrency-interest] reusing threads and thread local state

Gregg Wonderly gregg at cytetech.com
Wed May 4 22:45:45 EDT 2005

Mike Skells wrote:

>>I don't use threadlocals myself because it is just as easy to use a
>>Hashtable<Thread,Hashtable<String,?>> via a static factory which I can 
>>control access to and manage clearing etc on my own.

This is my statement, not Larry's.

> Interesting to know how you manage the cleanup, as you now have a strong
> reference to a Thread in you HashMap, so the Thread cannot be GC until every
> one of your explicit cleanups completes

I use explicit thread management to handle this issue.  And also, I use 
an periodic thread that asks if the thread is still running and remove 
entries for any such threads.

> Also all of the ThreadLocals that are created in that thread are not GCed
> either (fixed in JDK1.5).
> If this is run in an environment that manages Threads (outside you control)
> such as an appserver then the cleanup is complicated further.  

I don't use J2EE.  I just use J2SE and Jini for my enterprise 
applications.  Keeps me in control of the things that matter to me.

> Looks to me as if you have created your own personal memory leak!

Only if you don't understand all the issues and ways out.  There are a 
lot more "appliance programmers" who don't understand computer science, 
so these things will largely work for them.  But, these memory leak bugs 
are getting to be way to common (java.util.Timer cancelled tasks), so 
I've learned to just role my own rather than depending on bug free JDK 
code, which historically has not been available.

Gregg Wonderly

More information about the Concurrency-interest mailing list