• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuffer thread safe question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two pieces of code, both are asking how to ensure that instances of the class can be safely used by multiple threads?

1.

The answer to this question is Synchronize both log() and getContents() will make this class thread-safe

2.

The answer to this question is
It can be made thread safe if StringBuilder is replaced by StringBuffer Or
It can be made thread safe if addMessage() and dumpMessage() methods are synchronized.

My question is why I can not replace StringBuilder with StringBuffer to make first class thread-safe ? Thanks
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick answer to class 1:

The first code isn't threadsafe because a second thread could append data to contents concurrently, so "contents" could look like that:

one thread:
"Millis:nameMessage\n"

two threads (thread one in bold style):
"Millis:nameMillis:Messagename\nMessage\n

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it not because in the second example you are working with a static copy of the data?
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say the answer is in the title: for thread-safety, use a StringBuffer instead of a StringBuilder. That is, after all, their only (I think) difference.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:I would say the answer is in the title: for thread-safety, use a StringBuffer instead of a StringBuilder. That is, after all, their only (I think) difference.



I think he is asking why the answer did not recommend changing the type (in the first example) from StringBuilder to StringBuffer
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic