Reputation: 12575
I have a while loop to do string append using string buffer. What happens inside the jvm, since the append method is synchronized , does it check for lock every time on the object and proceeds when an append is called.
Upvotes: 0
Views: 530
Reputation: 308021
Yes, the synchronization of StringBuffer
is unnecessary on single-threaded code (or multithreaded code where the StringBuffer
is not shared between threads).
That's the reason why the StringBuilder
was introduced which has an identical API, but has non-synchronized methods.
Note, however, that the performance loss of those synchronized
keywords should be absolutely minimal, because uncontested locks are very, very fast in Java.
Upvotes: 3
Reputation: 147154
In some kind of theoretical sense the lock is acquired and released for every operation. In practice, for heavily used code paths, a lock coarsening optimisation is performed. So several operations in a row share the same lock acquire and release.
Also note that it tends to be the same thread using the lock (it's a per-instance lock). Typical JVM implementations will bias the lock for use from a particular thread, making the whole process surprisingly fast. As other have alluded, there is very little need to use StringBuffer
multithreaded (much the same for other microsynchronisation), so you might as well use StringBuilder
.
Upvotes: 1
Reputation: 533492
Even through StringBuffer is synchronized it is very difficult to use sensibly in multiple threads. i.e. if you are appending pieces of text in two threads like this.
thread A: stringBuffer.append("key1").append("= ").append("value1").append("\n");
thread B: stringBuffer.append("key2").append("= ").append("value2").append("\n");
Even though it is thread safe you can still get
key1key2= = value2\nvalue1\n
and many other combinations.
Upvotes: 0
Reputation: 23208
Yes, the general rules of using synchronized method applies here. Also, if you are in a single-threaded scenario, consider using a StringBuilder
and if you can't, at least try to use a Threadlocal version of your StringBuffer
.
Upvotes: 0
Reputation: 298838
Never mind that, just use StringBuilder
instead:
A mutable sequence of characters. This 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.
But to answer your question, read this: Java Tutorial > Synchronized Methods
Upvotes: 4
Reputation: 359786
If you're not writing multithreaded code, there is no reason to use StringBuffer
. Use StringBuilder
instead. Its API is identical, but StringBuilder
is not thread safe.
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 toStringBuffer
as it will be faster under most implementations.Instances of
StringBuilder
are not safe for use by multiple threads. If such synchronization is required then it is recommended thatStringBuffer
be used.
Upvotes: 2