Hello frnds.
i have little bit confusion on this topic, i try to find over the internet but not able to do this, could you please help me, i want to know that whether Calender.getInstance() method is threadsafe or not.

Thanks in advance

6 Years
Discussion Span
Last Post by ~s.o.s~

You should assume that all API methods are not thread safe unless the javadoc explicitly says they are. (Although in this particular case it's difficult to see what thread problems there could possibly be - the method has no side effects and doesn't update anything.)

Edited by JamesCherrill: n/a


> i want to know that whether Calender.getInstance() method is
> threadsafe or not.

AFAIK, yes, it is. Thread safe concerns normally crop up when you have destructive updates happening to an object. Calendar.getInstance() is a static method and doesn't alter any static fields of the Calendar class and hence is thread safe.

A word of advice though; just because a method is labeled thread safe doesn't mean you can use the corresponding class and always have correctness in your application. A classic example is the Vector class which has its `get`, `set, `size` methods synchronized but still has the possibility of exhibiting incorrect behaviour when used in your *custom* scenario. E.g.

if(vector.size() > 0) {
  // some complicated processing
  Element e = vector.get(0); // can fail!
  // Since by the time the control reaches here,
  // some other thread might have already modified the vector.

Edited by ~s.o.s~: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.