R
Robert Klemme
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