All entries for Wednesday 28 June 2006

June 28, 2006

DateFormat is not threadSafe… :(

Writing about web page

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() {

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….


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 :)

June 2006

Mo Tu We Th Fr Sa Su
May |  Today  | Jul
         1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30      

Search this blog



Most recent comments

  • Interesting… While I'm not completely convinced in such microbenchmarks, I'm pretty sure that 1ms … by Alexander Snaps on this entry
  • Hello. I bought the book yesterday. I was trying to find the source code for chapter 11 and chapter … by Suleman on this entry
  • by live mashup demo on this entry
  • Thanks mate ….. This blog was really helpful. by Maaz Hurzuk on this entry
  • Ty. Not directly helpful for my problem, but pointed me in the right direction. You will also get th… by Mike E. on this entry

Blog archive

Not signed in
Sign in

Powered by BlogBuilder