In the JSON-java library (org.json.JSONArray) I have found this code snippet with a synchronized
block around a method-local variable
public String toString(int indentFactor) throws JSONException {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0).toString();
}
}
I do not understand the necessity for the synchronization here, as the StringWriter
is only local to the given method (and, why the synchronization is on the Buffer). Is the synchronization really necessary here, and if, why?
buf = new StringBuffer();
inside the constructor. – Raising