Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between synchronization on StringBuffer and StringBuilder object ?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic