Is java.util.Calendar thread safe or not?

后端 未结 2 795
借酒劲吻你
借酒劲吻你 2020-12-10 01:43

I\'ve been working under the assumption that neither Date nor Calendar are thread-safe, but, during a recent discussion, a co-worker told me

2条回答
  •  甜味超标
    2020-12-10 01:56

    Documentation from Oracle says nothing about thread-safety: http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html.

    OpenJDK source code (build b147) implements java.util.Calendar in a non-thread-safe way, for example:

    public void setTimeInMillis(long millis) {
      // skipped
      time = millis;
      isTimeSet = true;
      areFieldsSet = false;
      computeFields();
      areAllFieldsSet = areFieldsSet = true;
    }
    

    I think that it's safe to assume that the class is not thread safe.

提交回复
热议问题