• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer and StringBuilder

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Quintin Stephenson
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still no response to the question posted above.


Thanks,
cmbhatt
 
swarna dasa
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right Swarna.


Thank you everybody for participating in this thread and helping me to
get the things in better way.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic