DateFormat is not threadSafe… :(
Writing about web page http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html
So under the assumption that DateFormat was threadsafe, I, and a number of my very experienced colleagues were doing the following:public class MyClass {
public static final DateFormat DATE_FORMATTER = .....
public void someMethod() {
DATE_FORMATTER...
}
}
and this is wrong wrong wrong :)
The javadoc clearly states that DateFormat and its implementations are not threadSafe and so usages of it need to be threadsafe.
If these had been merely class members instead of static then we probably would have never run into a problem because we have very few classes which are not instantiated once per request, but because these are static they are obviously shared by all instances of the class.
As to why DateFormat is not threadsafe…SimpleDateFormat (at least) stores a member calendar instance which it modifies during parse and format….
Joy.
Lesson is…read the javadoc of classes you think you know and be really sure anything that is static is threadsafe.
This was illustrated BTW by the use of IE7 on a page which used AJAX…. I can only think IE7 was issuing multiple requests at the same time? Who knows. And of course, debugging and stepping through didn't highlight the problem :)
Mathew Mannion
Oh no, they've made IE7 do something horrible, haven't they :(
28 Jun 2006, 23:35
Add a comment
You are not allowed to comment on this entry as it has restricted commenting permissions.