File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread safety difference between StringBuffer and StringBuilder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread safety difference between StringBuffer and StringBuilder" Watch "Thread safety difference between StringBuffer and StringBuilder" New topic
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: 449

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: 689

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.

.


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: 6662
    
    5

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 - More SCJP Mocks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

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 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread safety difference between StringBuffer and StringBuilder