Should I use StringBuilder or StringBuffer for webapps?
Asked Answered
O

5

8

I a writing a webapp in Java 1.6 and running it in tomcat. While I am not doing any explicit threading, I wonder about what is going on behind the scenes with Spring and Tomcat. Would I run into any issues using StringBuilder instead of StringBuffer?

Osbourn answered 23/8, 2012 at 20:5 Comment(3)
possible duplicate of StringBuilder and StringBuffer in JavaIcecold
@thinksteep This question is suppose to focus on what sort of threading happens behind the scenes in webapps and how that affects String building.Osbourn
Probably not a duplicate. This seems to be more of a question about threading in Tomcat than the StringBuilder/StringBuffer classes themselves.Autoxidation
A
12

If you are using a local variable you can safely use StringBuilder. Each thread will get its own instance.

Adopted answered 23/8, 2012 at 20:8 Comment(2)
+1. And if you are sharing strings between threads (e.g. by using static fields), then you should probably be using String rather than either StringBuilder or StringBuffer.Janae
To emphasize ruakh's point: passing immutable data between threads is the safe approach.Autoxidation
M
2

Usually Java EE components are not thread-safe by default, so unless you synchronize the blocks of code where you use the StringBuilder, you'll experience race-conditions. So, you either have to take care of synchronization or use StringBuffer.

Of course, as already mentioned if the StringBuilder is a local variable, you don't have to worry about that.

Maintenance answered 23/8, 2012 at 20:11 Comment(0)
A
2

Use StringBuilder since your builder is a local variable in doPost or doGet or else. It's true that multiple server threads use the same servlet instance but the fact that your builder is a local variable, there is no reason to worry ! If your builder was a member of your servlet class, sure you would get thread-safety problems. It's not your case i guess.

Assonance answered 14/11, 2013 at 15:59 Comment(0)
D
1

The Java doc says regarding java.lang.StringBuffer:

Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.

This means your operations on your StringBuffer will be safe even if you are in multi-thread environment [like web app]. The safety of your StringBuffer instance as such is governed by the scope of the variable.

Dirk answered 16/7, 2015 at 12:31 Comment(0)
F
0

if the code is in a Servlet (doGet/doPost) then multiple requests will cause the servlet instance to be multi-threaded. If the code is in a Spring bean it will depend on whether you configured the bean to be a singleton or prototype.

Fidellia answered 23/8, 2012 at 20:10 Comment(2)
To be more precise, you should say: that the servlet instance might be shared by multiple threads serving different clients.Maintenance
that is a better way of saying that. :)Fidellia

© 2022 - 2024 — McMap. All rights reserved.