• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized methods alone may not make a class thread safe- right?

 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

If I have a class as follows - ( Code taken from SCJP book page 764)



would it be right to say that the above class would not be thread safe if it also had a non private constructor that accepted a StringBuilder? Same if it was even a StringBuffer. Would that be correct?

Thanks,
Chan.
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic is resolved as I've found out the answer. The answer is yes, since I can send the same StringBuilder reference to different objects and since synchronized methods only block same instance's threads, having a public constructor that accepts the StringBuilder object would make this class unsafe for thread safety.

Thanks.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic