I have solved an issue about multithread processing at random. I'm happy because it works but I would like to know why. The faulty member in the code below is called INPUT_SDF
. I thought static final members didn't need synchronized block but when I remove it, everything goes wrong.
public class A implements Comparable<A>
{
public static final SimpleDateFormat INPUT_SDF = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.US);
...
public void setDate(String string) throws ParseException
{
synchronized (INPUT_SDF)
{
date = INPUT_SDF.parse(string);
}
}
}
Is my understanding of static final members wrong ? Or is there something else in my code not thread-safe ?