On 10/21/2012 4:43 PM, Osvaldo Doederlein wrote:
>
> "Unexpected memory retention" is the any kind of leak you can have in
> a garbage-collected language ;-)
Yes and no. If you have a static final field that points to a
collection, and you have a piece of code that adds an item to that
collection and never removes it, in my personal lexicon you don't have
unexpected retention -- you have a leak, and instead of having a
solution that is some (possibly huge) constant factor away from optimal
you have a solution that is simply untenable.
>
> This is a different case because SDF is clearly documented to not be
> thread-safe.
Who reads the manual? I'm pretty sure I didn't until FindBugs told me
how wrong I'd been, and I've been shouting "don't store a DateFormat in
a field" from the rooftops ever since, without a lot of people listening..
> I don't know every API by heart and I often write code with assistance
> of autocompletion, without checking the javadocs; but I never assume
> that some API is thread-safe unless I remember it clearly, or check
> the javadocs. I don't think the platform should go out of its way to
> protect that kind of sloppy coding.
Nobody gets thread safety right. And, lately, people have this idea that
they should be able to dependency-inject everything, either via Spring
or via a static constant... and the immediate response to "don't store a
DateFormat in a field", IF people are listening, is "wow, that sucks."
>
> Agreed that making the change you sugest would be convenient, but the
> problem is that the "bunch of fields" seems to be 10 fields (counting
> the non-final fields in JDK 7's impl); it's too much to pass around
> lots of internal methods via extra parameters. Maybe refactor all
> mutable fields and all internal methods that use them to a private
> worker class. This would resemble the alternative design, e.g.
> Pattern+Matcher, a two-layer system with a thread-safe parent object
> that does some preprocessing and a non-thread-safe, child worker.
> Maybe SimpleDateFormat should have adopted that design. Anyway,
> criticizing Java's Date APIs is a waste of time, it's a dead horse so
> maybe not the best example for your point ;-)
True, there's many other problems in j.u.Date and friends, starting with
all those deprecated methods in j.u.Date itself which are never going to
go away.