SimpleDateFormat is not thread-safe

July 11th, 2007 | Allen

It seems like a relatively common mistake is to assume that the java.text.SimpleDateFormat class is thread-safe (at least for methods such as format(), which you might not expect to mutate state!). This is not true; SimpleDateFormat is not thread-safe, and format() does mutate state. From the javadoc:

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

Suggestions on handling synchronization:


private final static ThreadLocal<SimpleDateFormat> shortTimeFormat =
     new ThreadLocal<SimpleDateFormat>() {
            protected SimpleDateFormat initialValue() {
                 return new SimpleDateFormat("HH:mm");
             }
      };

N.B. In general, you should be very careful when using ThreadLocal storage, especially when using thread pools (your data won’t be automatically garbage collected and may be visible to other Threads—a security risk) or when storing references to Thread objects in ThreadLocal storage (which might confuse the GC). However, in this case, we should be okay.

4 Responses to “SimpleDateFormat is not thread-safe”

  1. Jeff Says:

    You could also write a wrapper class that has a parse and format function which delegates to the member date format. I’d imagine that it’s not often that you’ll be using many additional methods and it’s not usually critical that your class be a true DateFormat since you’d rarely be substituting various DateFormat instances.

    I’ve got one called SynchronizedSimpleDateFormat.

  2. wismax Says:

    If the purpose is only to format dates, I suggest to use the FastDateFormat (or the DateFormatUtils static utility wrapper class). It’s a straightforward replacement for SimpleDateFormat, said to perform faster and thread-safe. By the way this library is full of other very usefull utilities, I always add it to my projet, you should give it a look !

  3. cupofjoe Says:

    I actually put a wrapper class around JProgressBar to make it thread safe. I was very annoyed that I couldn’t use it from a Swing Worker with informative text messages.

    Works like a charm now.

    Really have enjoyed the quality of this web site.

    Cheers.

  4. Li Says:

    I created a class to resolve this issue and address all problems comes with all alternatives disscussed above. I posted the class at my blog:
    http://li-ma.blogspot.com/

    Hopefully it can be helpful.

Leave a Reply


Palantir

Bad Behavior has blocked 416 access attempts in the last 7 days.