Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Synchronized methods alone may not make a class thread safe- right? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized methods alone may not make a class thread safe- right?" Watch "Synchronized methods alone may not make a class thread safe- right?" New topic
Author

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

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1021
    
  15
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
Bartender

Joined: Sep 06, 2012
Posts: 1021
    
  15
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized methods alone may not make a class thread safe- right?