aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer and StringBuilder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer and StringBuilder" Watch "StringBuffer and StringBuilder" New topic
Author

StringBuffer and StringBuilder

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

Could anybody please tell the case where we should care to use StringBuffer instead of StringBuilder because StringBuffer is thread safe. Or the thread
safety is only concerned with the internal working of method.

Please give any example to understand that on that particular place we should
use StringBuffer otherwise we may get inconsistent behavior.


Any help will be very appreciated.

Thanks,


cmbhatt
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
Hi there

This URL might help answer your question:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html

Cheers
Q


If at first you don't succeed, try, try again. If you don't try you have failed.
Sanjay Singh
Ranch Hand

Joined: Mar 02, 2006
Posts: 37

Hi Chandra, I have some doubt on your mock exam ques, could you give me any of your chat id(Gmail preferred). wanna to ask some doubt.


Regards, Sanjay Singh
SCJP-1.6, OCEWCD 6
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Quintin for providing the link!


Hi Sanjay,

Check your yahoo id, i just sent you a mail.
Send me a PM, I will get it on gmail a/c


Thanks,
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi everybody,

My doubt arose from the following question:




This class is used by multiple threads to add messages. Which of the given options correctly describe its behavior?
Select any two:

A: Messages added by multiple threads may not be stored correctly.
B: It can be made thread safe if StringBuilder is replaced by StringBuffer.
C: It can be made thread safe if only the addMessage() method is synchronized.
D: It cannot be made thread safe if method dumpMethod() is removed.
E: None of these is correct.



Source: Enthuware


Thanks,
[ May 04, 2007: Message edited by: Chandra Bhatt ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Still no response to the question posted above.


Thanks,
cmbhatt
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
All the addMessage function is doing is, appending something to the string builder.

I am not too sure what the source code would be like, but internally the append method would append each character of the string argument passed.

Since this method is not synchronized, 2 threads can be appending (perhaps adding characters in the for loop) at the same time.

So addMessage("abc") & addMessage("xyz") could append axbycz.

StringBuffer would always cause abc to be appended and only after that could xyz be appended or vice versa but never inter-mangle both.
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
I always think of StringBuilder and StringBuffer of holding data internally in a Collection of chars/bytes. That leads me to conclude it's answer A & B

A. Because we are using none synchronized methods and StringBuilder, multiple threads can simultaneously access the StringBuilders append() and toString () methods. This is not safe like Swarna Dasa pointed out.

B. Since the addMessage() and dumpMessage() methods contain only 1 line of code, replacing StringBuilder with StringBuffer will solve our thread-safety issue.

C. After synchronizing the addMessage() method, the dumpMessage() method will still be unsafe.

D. Bogus

E. Since we need to select 2 answers this could never be correct =)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Swarna and Custer for your prompt reply,


Since the addMessage() and dumpMessage() methods contain only 1 line of code, replacing StringBuilder with StringBuffer will solve our thread-safety issue.


Does this mean, one line of code will complete atomically with no other thread interference. I mean will it be executed by one thread and for that instance of time there wont be any effect of other threads.



Thanks,
[ May 04, 2007: Message edited by: Chandra Bhatt ]
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108


Does this mean, one line of code will complete atomically with no other thread interference


Yes, since the method is synchronized, the other thread which calls the method will have to wait for the lock on object.


I mean will it be executed by one thread and for that instance of time there wont be any effect of other threads.


what effect do you perceive? They would be waiting to get the object lock.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
All right Swarna.


Thank you everybody for participating in this thread and helping me to
get the things in better way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer and StringBuilder