• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Thread Safty With StringBuilder

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Base on what I've read is that StringBuilder (JDK 1.5+) isn't thread safe but better to use in a single-threaded application, where StringBuffer is thread-safe.
When using the "+" sign the compiler automatically create a StringBuilder, does it do the same if you write a multi-thread application? What is the bad things that can happen if this case?

so I understand the golden rule that for single-thread use StringBuilder, for multi-thread use StringBuffer.

thanks

Kind Regards,

Lennie De Villiers
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuilder has no synchronization.
StringBuffer has synchronization on methods and is "thread safe" (note the quotes).

For a call to an individual method, StringBuffer can be considered thread safe for those. But it is not thread safe if there are calls to more that one method. Those methods still need to be in a synchronize block.
So whether you use StringBuilder or StringBuffer in a multi-threaded application, you still must concern yourself with thread safety.
It's perfectly possible to use StingBuilder in a multi-theaded app without issue.

Bad things? Unexpected output and exceptions would be the bad things. Imagine two threads reducing the length of a StringBuffer and then trying to do stuff.
The StringBuffer starts out 10 characters long, and Thread 1 sets it to length-2 (so, 8)
Thread 1 gets put back to Runnable, Thread 2 becomes the executing thread and sets it to length-2 (so 6)
Thread 2 pauses and Thread 1 resumes, it tries to get the 8th character because it thinks the length is 8; it does not know that Thread 2 changed it to 6.
Can you see the problem?

And this is with the "thread safe" StringBuffer!
 
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When using the "+" sign the compiler automatically create a StringBuilder, does it do the same if you write a multi-thread application? What is the bad things that can happen if this case?



During string concatenation, the string builder is completely self contained. The code will create a string builder, use it to concat the strings, and then, get the resultant string. At no time, will this string builder instance be shared with another thread. It is completely "thread safe" because it will be used only by one thread.

Henry
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The only difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.



Reference: http://www.java-tips.org/
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukas Smith wrote:So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?


may be for optimization, cause concat() will create a new object for every midresulting String, and all them strings will reside in string pool after that.
That's just an assumption, i may be wrong.

With best regards
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukas Smith wrote:So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?



Generally, "why" questions require the Java designers to answer, as we don't know what they were thinking... but we can speculate. Possible answers are....

1. StringBuilder can chain. For example...



becomes...



Just create one string builder to create the resultant string.

2. StringBuilder can be used to handle non string types. For example...



becomes...



The string concat() method only does the concatenation of strings -- and then, only two at a time.

Henry
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Lukas Smith wrote:So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?



Generally, "why" questions require the Java designers to answer, as we don't know what they were thinking... but we can speculate. Possible answers are....

1. StringBuilder can chain. For example...



becomes...



Just create one string builder to create the resultant string.

2. StringBuilder can be used to handle non string types. For example...



becomes...



The string concat() method only does the concatenation of strings -- and then, only two at a time.

Henry



OK, thanks for reply, but concat(...) can be also chained but on the other hand it can take only String.
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukas Smith wrote:
OK, thanks for reply, but concat(...) can be also chained but on the other hand it can take only String.



Yes, but each link in the chain creates a new object. If you make a hundred calls to the append() method, you are still using the same string builder. With concat, you'll create a hundred intermediate string objects.

Henry
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, indeed - Strings are immutable and concat(...) returns new String object.
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic