以下是Java 7中
Calendar和
GregorianCalendar源代码的链接
如果您读取代码,您将看到没有一个实例方法是同步的,并且没有一个实例字段是volatile的.你也会看到,即使这个字段获取方法也可能导致一个Calendar实例变异.并且由于没有执行同步,不同的线程可能会在这种突变操作之后看到Calendar对象的字段的陈旧版本.
为了记录,字段中的变异动作获取方法发生在/在调用此方法期间:
1555 protected void complete()
1556 {
1557 if (!isTimeSet)
1558 updateTime();
1559 if (!areFieldsSet || !areAllFieldsSet) {
1560 computeFields(); // fills in unset fields
1561 areAllFieldsSet = areFieldsSet = true;
1562 }
1563 }
简而言之,Calendar类不是线程安全的,GregorianCalendar不是因为它继承了非线程安全的字段和方法.
但不要只是为了我的话.做自己的源代码分析.
And, to top it off, the documentation doesn’t say anything one way or another, not for Calendar, nor even for Date.
如果javadoc不指定类的线程安全性,则应该假定它不是线程安全的.