I have a scenario where I want to use StringBuilder as a local variable in a method. I understand if StringBuilder is a local method variable it should not have any problems about thread-safety.
However, if I append to the StringBuilder an instance variable like:
class MyClass {
private List<String> property;
public void myMethod() {
StringBuilder sb = new StringBuilder();
for(String s : property) {
sb.append(s);
}
}
// some other methods that mutate property
}
I think to make this thread-safe, simply change the StringBuilder to StringBuffer is not sufficient. Shall I synchronize on the property itself?
List, not theStringBuilder