I have two pieces of code, both are asking how to ensure that instances of the class can be safely used by multiple threads?
1.
The answer to this question is Synchronize both log() and getContents() will make this class thread-safe
2.
The answer to this question is
It can be made
thread safe if StringBuilder is replaced by StringBuffer Or
It can be made thread safe if addMessage() and dumpMessage() methods are synchronized.
My question is why I can not replace StringBuilder with StringBuffer to make first class thread-safe ? Thanks