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

Recommended Answers

All 2 Replies

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.)

> 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.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.