今天突然想起来上次面试的时候被问到SimpleDateFormat是不是线程安全的,那时我的想法是,这东西设计成线程安全应该很容易,为什么不设计成线程安全的呢,接着又想,就算不是线程安全的,一个线程new一个SimpleDateFormat不就行了。

     今天看了下源码

 
  1. public Date parse(String text, ParsePosition pos) 
  2.     { 
  3.      
  4.         checkNegativeNumberExpression(); 
  5.      
  6.         int start = pos.index; 
  7.         int oldStart = start; 
  8.         int textLength = text.length(); 
  9.  
  10.         calendar.clear(); // Clears all the time fields
  11. ….. 

calendar.clear(),注意这句,calendar是类的一个protected成员变量,然后就果断不是线程安全的了…