今天突然想起来上次面试的时候被问到SimpleDateFormat是不是线程安全的,那时我的想法是,这东西设计成线程安全应该很容易,为什么不设计成线程安全的呢,接着又想,就算不是线程安全的,一个线程new一个SimpleDateFormat不就行了。
今天看了下源码
- public Date parse(String text, ParsePosition pos)
- {
- checkNegativeNumberExpression();
- int start = pos.index;
- int oldStart = start;
- int textLength = text.length();
- calendar.clear(); // Clears all the time fields
- .....
calendar.clear(),注意这句,calendar是类的一个protected成员变量,然后就果断不是线程安全的了...