• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Thread safety StringBuffer examples

 
Rancher
Posts: 159
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I trying to understand how does thread safety works. So StringBuffer is thread safe and StringBuilder is not. How can I simulate the wrong behavior with StringBuilder and then check right behavior of StringBuffer?

I found some example on StackOverflow but I do something wrong, because the output is correct:

 
Marshal
Posts: 73244
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always tell us where you found such code; in this case we would need the exact link please.
That example is incomplete; it doesn't show different thre‍ads accessing the sb field. Also, try changing the StringBuilder to a StringBuffer object because StringBuffer is actually thre‍ad‑safe.
 
Mike Savvy
Rancher
Posts: 159
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please always tell us where you found such code; in this case we would need the exact link please.
That example is incomplete; it doesn't show different thre‍ads accessing the sb field. Also, try changing the StringBuilder to a StringBuffer object because StringBuffer is actually thre‍ad‑safe.


Okay, link here -> https://stackoverflow.com/questions/26860173/what-does-stringbuilders-are-not-thread-safe-mean
But my post is more about my question and less about this code.
 
Campbell Ritchie
Marshal
Posts: 73244
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that SO post was clear and easy to understand. Please show us how you created the multiple thre‍ads.
 
Master Rancher
Posts: 3929
51
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm less impressed by the Stack Overflow post, particularly the part that gave us the addProperty() method discussed here.  The problem is that whether or not the individual methods are thread-safe (yes for StringBuffer, no for StringBuilder), the overall code gives unpredictable results  because you need more than one append() call to add a property, and there is nothing preventing two different threads from interfering with each other.  Even if we use a "thread safe" StringBuffer, the code using the StringBuffer is not thread safe.  So it's a confusing example.

Mike, regarding your original question, I would say that in general it's often pretty difficult to *demonstrate* a lack of thread safety.  Most of the time if you write unsafe code, it will probably work fine, most of the time.  But sometimes, a problem may occur. And when it does occur, it tends to be hard to understand the cause, especially since we can't reliably reproduce the problem.  
 
Bartender
Posts: 1204
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I'm less impressed by the Stack Overflow post, particularly the part that gave us the addProperty() method discussed here.  The problem is that whether or not the individual methods are thread-safe (yes for StringBuffer, no for StringBuilder), the overall code gives unpredictable results  because you need more than one append() call to add a property, and there is nothing preventing two different threads from interfering with each other.  Even if we use a "thread safe" StringBuffer, the code using the StringBuffer is not thread safe.  So it's a confusing example.

Mike, regarding your original question, I would say that in general it's often pretty difficult to *demonstrate* a lack of thread safety.  Most of the time if you write unsafe code, it will probably work fine, most of the time.  But sometimes, a problem may occur. And when it does occur, it tends to be hard to understand the cause, especially since we can't reliably reproduce the problem.  



Agreed about the SO post.

In any but the nicest multi-threaded environment, the code given above probably won't give you what you want consistently.

If you call main() from two different threads the first one is likely to output "1=2,a=b,c=d,e=f" and the second one will give you "1=2,a=b,c=d,e=f,a=b,c=d,e=f".  (...where the bolded output comes from the second thread.)

The second thread might take control between the addProperty() calls in main.  The second thread could complete first with output, "1=2,a=b,a=b,c=d,e=f" and then the first thread would restart and complete with "1=2,a=b,a=b,c=d,e=f,c=d,e=f"

The second thread might even interrupt between append() calls in addProperty().  Second thread: "1=2,a=b,c,a=b,c=d,e=f"  First thread: "1=2,a=b,c,a=b,c=d,e=f=d,e=f".

And of course there are other possibilities as well.

If you do indeed want the StringBuilder/StringBuffer to start with "1=2" and then have one copy of the other properties for each thread that runs main(), you could make main() thread-safe by making the chunk of code that calls addProperty() synchronized.  Google "java synchronized".



 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic