![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301889.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301333.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
安全的。如下给出证明:
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302435.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302435.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302435.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302435.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302435.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302435.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301889.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301333.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101303556.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301889.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301333.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101303556.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301889.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301333.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301367.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301691.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301540.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101301927.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101302109.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200703/20070301101303556.gif)
很快,基本几十次就会出现错误。错误原因:
因为SimpleDateFormat处理复杂,Jdk的实现中使用了成员变量来传递参数,这个就造成在多线程的时候
会出现错误。上边的程序证明了这个问题。
再看看SimpleDateFormat的Jdk 的Source,有这么一段注释,说明它不是线程安全的。
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
继续看看Sun自己的网站上。在sun的bug database中
Bug ID: 4228335 讲的就是这个问题。
SimpleDateFormat is not threadsafe (one more try)
其中有这么几段话值得关注:
1,
Aside from the obvious, there are two reasons why this fix should be made:
- The documentation for DateFormat states that a DateFormat object should be used
multiple times, implying that construction is expensive. Furthermore,
no mention is made of SimpleDateFormat's lack of thread-safety.
Since for most applications the date formats remain constant,
it is very easy to conclude that DateFormats should be application globals.
But SimpleDateFormat produces incorrect results when used in this way.
- Bug 4101500, a similar problem with NumberFormat, was fixed.
建议修改这个问题,而且NumberFormat已经修改,解决了这个问题。简单测试了一下NumberFormat确是不出错
2,
Use of a cloned Calendar object in format(), as suggested in JDC comments,
slows down the execution 30-50%. And it potentially affects footprint because
of cloned objects. Another approach would be to have a Calendar instance per
thread. However, this approach will cause problems at the API semantic level due
to the get/setCalendar methods.
这一段解释了为什么没修改这个问题,一个是保持API不变,另一个是因为Clone比new慢,会损失效率
关于clone与new的快慢,看这里http://www.blogjava.net/dreamstone/archive/2007/02/26/100761.html。
其它的还有好多,感兴趣的自己看看吧。
结论:每次使用它的时候现new,或者用一个同步函数把new好的包装起来使用吧。