DateFormat is not thread-safe

6 AM August 19, 2005

One reason for having a weblog is that it makes a handy notebook of important things to remember. For instance, I cut-and-paste from this post on reconfiguring log4j every few months.

So, to help my memory, I now committing to the Interweb the fact that DateFormat is not thread-safe, and neither is SimpleDateFormat, and I'm not the first to do so.

Now, excuse me while I code a small patch for a production application...

By alang | # | Comments (5)
(Posted to javablogs and Java)

Comments

At 10:37, 19 Aug 2005 Glen Stampoultzis wrote:

It is an easy thing to overlook. I just wish I understood why they didn't make it thread safe. Seems to me that it is the sort of thing you would expect to be threadsafe.

(#)
At 10:45, 19 Aug 2005 Daniel Bradby wrote:

There was a good article in Dr Dobbs about this, some potential fixes, and how well they work. The best approach was to use thread locals over synchronized methods.

(#)
At 14:39, 19 Aug 2005 Michael Slattery wrote:

Here is a thread safe wrapper:
http://www.jsourcery.com/output/apache/james/2.20/org/apache/james/util/SynchronizedDateFormat.html

I know there is a least one more OSS library that has a threadsafe wrapper implementation of DateFormat.

It IS reported as a bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4228335

But, the JavaDoc does warn about thread issues. RTFM.

(#)
At 06:25, 24 Aug 2005 Jed Wesley-Smith wrote:

FastDateFormat from commons-lang, thread-safe and built for speed.

(#)
At 22:58, 07 Sep 2005 Alan Green wrote:

Nice find, Jed. Unfortunately, FastDateFormat only formats dates, it doesn't parse them.

(#)

Add Comment




(Not displayed)






(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)




© 2003-2006 Alan Green