DateFormat objects and thread-safety
July 30, 2007 9:29 PMOne of my daily responsibilities is to look over the code that is checked into our JIRA source base. I look at every commit and check for any obvious mistakes, typos or things that can be instantly improved.
I must admit that in recent months it was and still is quite hard to pick any obvious errors or coding mistakes from our JIRA code. I hardly get to find something major that I would need to report back to the author of that code check-in.
Recently I blogged internally about some minor findings. Some of these were not recent mistakes, rather the code that I recently touched (as part of changing some other parts in JIRA), the old code that exposed some of the caveats of Java that still live inside JIRA source base.
I received a very positive feedback to my post. This blog post also sparked a discussion between developers (including developers from other teams). I would like to sum this discussion up and share it with you.
In my post I suggested to use DateFormat class only locally. The reason behind it is that the implementation of DateFormat class is not thread-safe. As it's JavaDoc states:
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.
The problem with this class is that it has a state and if you have two or more threads calling methods on the same instance you could get results you would not expect. As commented by Charles Miller:
"SimpleDateFormat's non-thread-safety can cause some really fun bugs, because the most common symptom is getting correctly formatted output for a totally different date."
I suggested that the simplest fix for the concurrency problem is to create a new instance each time as needed. This naturally is not the best solution from the performance point of view, but the simplest trade-off for thread safety.
This was rightfully spotted by Adrian Hempel, who suggests to use ThreadLocal to store a reference to DateFormat object. He also warns about premature optimization and adds that this should be only used if a construction of a new object each time becomes a real problem.
Another very good suggestion came from Mark Chaimungkalanont who suggests to use FastDateFormat from Apache Commons Lang package. This class is thread-safe therefore can be happily used across multiple thread.
While we are on the topic of DateFormat objects, it's worth to mention that these objects are serializable (implement Serializable interface) and it may not be a good idea to store their instances as member variables. As Adrian also mentioned to us:
"A serialised SimpleDateFormat is 36K in size! GregorianCalendar has a similar but lesser problem, making its serialised form 2.3K. The reason is that they both include all their constant data relating to time zone in their serialised forms."
Ok, I hope I did not bore you too much with this lesson on DateFormat objects. We like to share information, between the developers internally or externally. As we discovered many times it's a great way to learn more.
If you'd like to work with us, Atlassian is hiring. And don't be surprised if you happen to be asked about thread-safety of DateFormat objects at the tech interview :-)



Copyright © 2009 Atlassian Pty Ltd.

6 Comment(s)
We are using joda-time library that solves "not thread safe DateFormat" problem and has some more good Date related things...
By Den at July 31, 2007 3:49 AM
Good article.
Don't forget the other subclasses of Format aren't thread safe either. I had a doozy of a bug when parsing numbers from Strings using DecimalFormat. It was returning numbers in the millions when the Strings were clearly in the thousands. Took me some time to discover this bug as a debugger isn't very useful in thie situation.
By Eugene at August 1, 2007 11:16 PM
Nice entry. Just wondering, is Atlassian always hiring? I thought Atlassian just bought over Cenqua, so there must be quite a lot of employees altogether now ... isn't that enough yet?
All the best you the folks at Atlassian and Cenqua. Cheers. :)
By tmjee at August 12, 2007 11:12 PM
Thanks tmjee!
I cannot really comment on our hiring processes. All I can say is that the aquisition of Cenqua brought us as many developers as it brought us new products (so to speak). That means we have more products to work on and support.
By Dushan Hanuska at August 13, 2007 2:12 AM
Absolutely, we're still hiring. Atlassian is around the 100-employee mark worldwide now, but there's still a lot of work to be done, and we're still looking for more people to help us do it.
http://www.atlassian.com/about/jobs.jsp
By Charles Miller at August 13, 2007 6:22 AM
I created a class to resolve all the issues related to DateFormat.
I feel lots of people are having the same problem, so I posted to my BLOG:
http://li-ma.blogspot.com/
Hopefully it can be useful.
By Li at October 18, 2007 1:07 PM