I am building up a String out of multiple pieces and want to use either StringBuffer or StringBuilder to do so. From the Java 5 docs, I see that StringBuilder is preferred when possible, with the caveat that "Instances of StringBuilder are not safe for use by multiple threads". From this statement I understand that I should not have a single StringBuilder instance shared by multiple threads. But what about this case:
//Is this safe?
//foo() is called simultaneously by multiple threads
String foo(String a, String b) {
return new StringBuilder(a).append(b).toString();
}
Here there could be multiple threads in the function at the same time, using the StringBuilder class at the same time (eg, concurrent access of static variables, if there are any), but each thread would have its own separate instance of StringBuilder. From the documentation I can not quite decide whether this counts as use by multiple threads or not.
Any help to clarify this would be appreciated.