Tuesday, July 06, 2004
SimpleDateFormat isn't thread safe?
You've probably heard the story before - you're using SimpleDateFormat to produce some human-readable output from a Calendar object. Your JUnit test cases all look great. The integration tests all look great.
Then, the code migrates to the QA environment. Strange scheduling things occur. Your QA guy starts asking the "gee, I don't want to come out and say it, but did you actually test any of this?" questions.
After a bit of poking around, there is little doubt in your mind that gremlins have taken control of the environment and are torturing you just for fun.
Sometime around 3am, you decide to pore over the Java docs...and there it is -- buried.
"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. "
A quick Google tells you the same thing, along with the sob stories of other poor souls like yourself.
So, without dragging this on ad nauseum, how is it that the java.text.Format packages are not thread-safe? There is an alternative in Commons, but geez, 5 hours of my life I won't get back anytime soon...
Then, the code migrates to the QA environment. Strange scheduling things occur. Your QA guy starts asking the "gee, I don't want to come out and say it, but did you actually test any of this?" questions.
After a bit of poking around, there is little doubt in your mind that gremlins have taken control of the environment and are torturing you just for fun.
Sometime around 3am, you decide to pore over the Java docs...and there it is -- buried.
"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. "
A quick Google tells you the same thing, along with the sob stories of other poor souls like yourself.
So, without dragging this on ad nauseum, how is it that the java.text.Format packages are not thread-safe? There is an alternative in Commons, but geez, 5 hours of my life I won't get back anytime soon...
