This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 1000
    
  16
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: 1000
    
  16
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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Synchronized methods alone may not make a class thread safe- right?
 
Similar Threads
Problem with thread question from K & B
Threads question.
StringBuffer thread safe question
threads problem
a synchronization question from the B SCJP 6 book