JDK-6209042 : (thread spec) Clarify when ThreadLocal.finalize() runs
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-12-15
  • Updated: 2010-04-02
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
The current documentation does not make it clear when the finalize() method of ThreadLocal runs. Does it run once per thread shutdown or does it run once when the enclosing object dies (assuming no other external references)?


URL OF FAULTY DOCUMENTATION :
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ThreadLocal.html
###@###.### 2004-12-15 17:37:40 GMT

Comments
EVALUATION A clarification: ThreadLocal does not have a finalize() method, but it does use WeakReference, which involves the special machinery in the GC for finalizable references. --- It's easy to overspecify this by documenting the current implementation in the JDK, but we're looking for better ways to reclaim ThreadLocal memory, and we wouldn't want to specify our current possibly suboptimal solution. We *do* want to encourage the use of ThreadLocal.remove().
15-11-2007

EVALUATION A ThreadLocal object itself is eligable for GC when it becomes unreferenced. The submitter should consult Object.finalize to learn about that method. What happens to references to the state held by a ThreadLocal for a given thread when that thread exits is implementation dependent (when the thread returns from its run method it nulls the references to maps for regular and inherited locals). Consideration will be given to explaining how to avoid leaks involving locals and perhaps adding an implementation note covering the above behavior. ###@###.### 2005-05-04 20:16:15 GMT
04-05-2005