SimpleDateFormat is not thread-safe
July 11th, 2007 |
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:
- Use the Joda Time libraries if possible. They are thread safe. Joda Time is fairly easy to port to from existing JDK compatible code.
- Synchronize calls to format() manually. You probably want to write a wrapper class/function to do this, and make sure nobody calls the original format() method by accident. This seems like it should be safe, but see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=422833.
- Use thread-local storage, e.g.
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.






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.
July 12th, 2007 at 12:52 pm
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 !
August 7th, 2007 at 9:22 pm
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.
September 20th, 2007 at 2:48 pm
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.
October 18th, 2007 at 12:08 pm