This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between synchronization on StringBuffer and StringBuilder object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between synchronization on StringBuffer and StringBuilder object ?" Watch "difference between synchronization on StringBuffer and StringBuilder object ?" New topic
Author

difference between synchronization on StringBuffer and StringBuilder object ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

please consider the following code :





here i'm locking on StringBuffer object which has synchronized methods. i would like to know what if i use StringBuilder object. I tried running the code using Stringbuilder and got the same output. i would like to know is there any difference between the two scenarios in terms of thread-safety, synchronization etc. ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20050
    
  44

gurpeet singh wrote:please consider the following code :





here i'm locking on StringBuffer object which has synchronized methods. i would like to know what if i use StringBuilder object. I tried running the code using Stringbuilder and got the same output. i would like to know is there any difference between the two scenarios in terms of thread-safety, synchronization etc. ?


The StringBuffer class methods are synchronized. The StringBuilder class methods are not synchronized. Based on the way your application uses the string buffer/builder, this shouldn't make any difference.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Have you checked out Aspose?
 
subject: difference between synchronization on StringBuffer and StringBuilder object ?
 
It's not a secret anymore!