javadoc:
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.
在项目中定义了一个处理时间的类:DateTimeUtility.java
定义了若干静态方法 ,如:
public static int compare(String leftDateString, String rightDateString) {
int result = 0;
try {
Date leftDate = null;
Date rightDate = null;
//synchronized (isoDateformat) {
leftDate = isoDateformat.parse(leftDateString);
rightDate = isoDateformat.parse(rightDateString);
// }
if (leftDate.equals(rightDate)) {
result = 0;
}
if (leftDate.after(rightDate)) {
result = 1;
}
if (leftDate.before(rightDate)) {
result = -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
使用多线程的情况下,使用此函数可能造成比较结果不正确,跟踪结果表明:isoDateformat.parse(leftDateString)这段代码会不确定性的出现结果错误,比如:传入参数为2011-01-24,parse出来的结果可能是2011-01-05。加上红色部分代码可以解决此问题
目前尚未测试单线程下连续调用是否会有问题