Re: Proposed new Java feature
On 28.05.2012 21:16, markspace wrote:
On 5/28/2012 10:29 AM, Robert Klemme wrote:
However I think about it: the idea of simply clearing ThreadLocal
references still does not become a good idea. The creator of a
ThreadLocal needs to consider how it is used and how cleanup is done.
Everything else introduces dangerous side effects which lead to bugs
which are at least as hard to find as those memory leaks.
Naw, I just disagree. Saying that the creator is responsible for cleanup
is like saying that because some objects need an explicit "close," we
should force those semantics onto all objects.
The cleanup method does not need to be overridden - that's why I did not
make it abstract. There is no semantic forced on all objects.
Also, I don't see the implication: If clearing references is sufficient
then that's perfectly OK. But, as you noticed, there are objects which
require some explicit cleanup. Only the author of the code which
creates a ThreadLocal can know how it must be cleaned up. With the
global reference nulling you are actually forcing semantics on all
because then there is just this one way. Since
ThreadLocal.initialValue() is there for custom initialization, it's as
reasonable to provide a hook for resource deallocation code. (Btw.,
that's the same pattern callback interfaces for object pools employ.)
If one does not want the cleanup hook for all ThreadLocals, one can
place the cleanup functionality in a sub class of ThreadLocal thus not
affecting existing code at all.
If you have a thread local that requires special semantics, go ahead and
provide the special code for those objects. The rest of the code can use
a common cleanup up mechanism that MOST objects require: just GC them.
The point is that with the global reference cleanup the specific code
does not have a chance to run because it does not know when. The proper
point in time is just before or during remove().
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/