| Author |
Thread safety difference between StringBuffer and StringBuilder
|
Sivakumar Janardhanan
Ranch Hand
Joined: Dec 11, 2008
Posts: 30
|
|
Hello All, Good Morning
I have an one doubt in StringBuffer and StringBuilder class.
Generally StringBuffer class is a synchronized one. it will perform thread safety operation.
But StringBuilder is no synchronized operation.
now tell me
1. why we say StringBuffer is a thread safety?
2. where ll we use the StringBuffer with Thread?
Please tell me the difference between those two classes and give me the good example.
Thanks.
|
 |
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
|
|
StringBuilder was introduced in Java 1.5
Sivakumar Janardhanan wrote:why we say StringBuffer is a thread safety?
b/c it has designed in that way The operations are same as stringbuilder but using synchronized keywords
Sivakumar Janardhanan wrote:2. where ll we use the StringBuffer with Thread?
If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.
|
http://muhammadkhojaye.blogspot.com/
|
 |
Nishan Patel
Ranch Hand
Joined: Sep 07, 2008
Posts: 676
|
|
Hi,
As you know StringBuffer is synchronized so only one time one thread can access StringBuffer object. SO when you are accessing StringBuffer object no one can change and use this object. So second thread not modify the object of StringBuffer and that is why it's tread safe.
.
|
Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
|
 |
Atwal Usha
Ranch Hand
Joined: Sep 10, 2009
Posts: 137
|
|
The only difference between StringBuffer and StringBuilder is that StringBuffer is synchronized whereas StringBuilder is not.
Thus, when a single thread is to be used to run the application it is better to use StringBuilder and if the application is to be accessed from multiple threads, StringBuffer should be used because of its synchronized feature.
|
Java Certification Exam Mock Tests: SCJA SCJP 5 SCJP 6 SCJP 6 (Online Training) SCJP 6 (Instructor Led Training) SCWCD 5 SCBCD 5 SCEA 5
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6590
|
|
|
A thread safe class ensures that the class's internal state is accessed safely by multiple threads. However using a thread safe class need not necessarily mean that your program is acting in a thread safe way. It simply means that the object in question will execute operations in a thread safe manner
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
You could argue that it was a design mistake to make the StringBuffer class synchronized. It's synchronized to make it thread-safe, which means that if there are multiple threads that try to append data to the same StringBuffer object, you're sure that it doesn't get into an inconsistent state.
In practice, programs almost never use one StringBuffer object in multiple threads at the same time, so for most programs the synchronization is not necessary and only adds overhead.
That's why in Java 1.5 Sun added StringBuilder - the same thing as StringBuffer, but without the synchronization overhead that's unnecessary for most programs.
You should prefer to use StringBuilder, and only use StringBuffer if you really are using the object from multiple threads at the same time. And even then you could use StringBuilder and do the synchronization yourself.
StringBuffer was not removed from the Java API because there are lots of old programs that use it and Java must remain backward compatible.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Thread safety difference between StringBuffer and StringBuilder
|
|
|