3

After reading this - What does 'synchronized' mean? I was still unable to understand why StringBuffer would be slower than StringBuilder in a thread-safe environment. What extra time-consuming work does StringBuffer have to do that makes it slower?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Shailesh Tainwala
  • 6,299
  • 12
  • 58
  • 69
  • 2
    You answer your own question. The StringBuffer is synchronized so it has to do more work that the other one does have to do. What makes it worse in this case is that 99% of the time its not needed. – Peter Lawrey Jun 09 '11 at 14:02
  • 1
    ... and the thread that he links to pretty much explains why synchronization costs a bit more than code without synchronization. – Kaj Jun 09 '11 at 14:05
  • Since the focus of the previous question was not on what extra work a synchronized method does, I had to assume there may be more than what was mentioned in the answers (to justify the very vehement 'Do not use StringBuffer unless you need Synchronization' statements I have come across). The selected answer below gives the information I needed. I think the question is justified. – Shailesh Tainwala Jun 10 '11 at 08:56
  • might be interesting: [How do I prove programmatically that StringBuilder is not threadsafe?](https://stackoverflow.com/questions/48558432) – Andrew Tobilko Feb 19 '18 at 10:18

3 Answers3

7

There is some small overhead acquiring and releasing even an uncontended lock, and lock elision won't work in StringBuffer even if most instances are not used cross-thread because an instance could be.

See http://book.javanb.com/java-threads-3rd/jthreads3-CHP-5-SECT-1.html for a description of what the VM has to do when acquiring and releasing locks.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
2

Making sure things are running in synch. Or, more to the point, out of synch. Synchronizing a method call means two different invocations of that method (on that object if it is not static) have to take turns entering the method. Thread B cannot enter method synchMeth until Thread A (already in the method) has finished.

The checks for whether a synchronized block has been locked or not, and by whom, take extra time.

Atreys
  • 3,741
  • 1
  • 17
  • 27
2

Read this from JavaDoc

StringBuilder class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

You must read this article on StringBuffer vs. StringBuilder performance comparison

As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, {StringBuilder}. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

Additional Useful Link : Difference between StringBuffer and StringBuilder class.

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164