| Author |
Thread Safty With StringBuilder
|
Lennie De Villiers
Greenhorn
Joined: Jul 17, 2009
Posts: 24
|
|
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
Joined: Jun 09, 2009
Posts: 327
|
|
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!
|
SCJP6
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Stephen Davies
Ranch Hand
Joined: Jul 23, 2008
Posts: 352
|
|
Reference: http://www.java-tips.org/
|
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?
|
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
|
 |
Anastasia Sirotenko
Ranch Hand
Joined: Jul 20, 2009
Posts: 64
|
|
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
|
[SCJP 6.0]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
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
Joined: Apr 20, 2009
Posts: 804
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
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
Joined: Apr 20, 2009
Posts: 804
|
|
|
Yes, indeed - Strings are immutable and concat(...) returns new String object.
|
 |
 |
|
|
subject: Thread Safty With StringBuilder
|
|
|