I'm looking at legacy code and found the following:
private static final SimpleDateFormat sdf = new SimpleDateFormat("...");
...
void foo() {
bar(date, someMoreArgs, sdf.clone());
}
where bar()
then goes ahead and uses the passed SimpleDateFormat
to format the given date.
Is the above code thread-safe? If multiple threads concurrently call sdf.clone()
, can one of the cloned objects end up getting corrupted?
I wouldn't write the code like that myself in the first place. I know there are better ways to do this. But I'm not looking to refactor the code unless it can be proven to be not thread-safe.
Edit:
Some more information for clarification:
The static object
sdf
itself is never used for formatting. The only operation it's ever used for is cloning. Thus, I'm not expecting its contents to change (unless the cloning operation writes some transient data inside the object).The clone is never used by more than one thread.