Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Safty With StringBuilder

 
Lennie De Villiers
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jason Irwin
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Stephen Davies
Ranch Hand
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • 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/
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, indeed - Strings are immutable and concat(...) returns new String object.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic