aspose file tools*
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: 18896
    
  40

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between synchronization on StringBuffer and StringBuilder object ?