aspose file tools*
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
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: 839
    
  14
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: 839
    
  14
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?
 
Similar Threads
a synchronization question from the B SCJP 6 book
threads problem
StringBuffer thread safe question
Problem with thread question from K & B
Threads question.