Thursday, January 08, 2009

Class SimpleDateFormat is not thread safe

One tiny find this week from our team,during I run the load testing and there is one step we need to fill the target date into the text box.

in our code, we need to parse date/time format using SimpleDateFormat class, so if IthreadA is parsing a value while threadB is changing the pattern, then error message bellowing could happen,"occasionally":
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:424)
at java.lang.Long.parseLong(Long.java:461)
at java.text.DigitList.getLong(DigitList.java:167)
at java.text.DecimalFormat.parse(DecimalFormat.java:1271)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1375)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1124)
at java.text.DateFormat.parse(DateFormat.java:333)
at com.XXXX.struts.DateConverter.convert(DateConverter.java:41)
at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
So, we just simply change the definition of SimpleDateFormat object from class variable to local variable, this problem have been solved!

"This is a pitfall from JDK" :) I am not sure how many of you already release it....
You can look at the interesting article from Brian Goetz
http://www.ibm.com/developerworks/java/library/j-jtp09263.html

No comments:

Post a Comment