Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread safety difference between StringBuffer and StringBuilder

 
Sivakumar Janardhanan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


1. why we say StringBuffer is a thread safety?



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.

.

 
Atwal Usha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15494
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic