I am writing a private utility method in a Spring Controller.
I need to use either StringBuffer or StringBuilder. The former is thread-safe, the latter is not, but the latter is much faster.
Basically, it looks like this:
private String buildTextToDisplay() {
StringBuffer sb = new StringBuffer();
sb.append( ... );
return sb.toString();
}
Obviously, I don’t want two sessions to call this simultaneously and return garbage.
Since
sbis a local variable, it doesn’t need to be thread safe. (Each thread would have its own reference to a uniqueStringBuilder, right?)