Using a pattern very similar to that described in a recent question, for a multithreaded application, I am getting weird date values (e.g., years like 2025 or 2035, when clearly no such value exists in the source data). It seems that a concurrency issue is occuring.
The source code looks something like
// Various Java DateFormat patterns, e.g. "yyyy-MM-dd".
private static final String[] DATE_PATTERNS = new String[] {...};
private static SimpleDateFormat[] getFormats(final String[] patterns)
{
ThreadLocal<SimpleDateFormat[]> LOCAL_FORMATS = new ThreadLocal<SimpleDateFormat[]>()
{
@Override
protected SimpleDateFormat[] initialValue()
{
List<SimpleDateFormat> formatList = new ArrayList<SimpleDateFormat>();
for (String pattern:patterns)
{
formatList.add(new SimpleDateFormat(pattern));
}
return formatList.toArray(new SimpleDateFormat[formatList.size()]);
}
};
return LOCAL_FORMATS.get(); // Create a thread-local copy
}
private static final SimpleDateFormat[] DATE_FORMATS = getFormats(DATE_PATTERNS);
After its static initialization, the DATE_FORMATS
array is accessed by numerous classes, which in turn use the SimpleDateFormat
objects of the array for parsing or formatting several date strings.
Can there be any concurrency issue in such a usage scenario, especially given the use of ThreadLocal
?