I have a very basic question. I have read that if we have a multi-threaded application then it is better to go with StringBuffer
. But if we have a single thread application then it’s better to go with StringBuilder
.
But isn't the whole point of having a multi-threaded application is that simultaneously all the threads can work on the same piece of code? Shouldn't the requirement be that if we don't want the String to be manipulated by all the threads at a time, then we should go for StringBuffer
, otherwise it's fine to go for StringBuilder
? What I am trying to understand is for a multi-threaded application why is it required to go for a synchronized implementation (if the requirement doesn't state that).
Shouldn't the requirement be that if we don't want the String to be manipulated by all the threads at a time, then we should go for StringBuffer, otherwise it's fine to go for StringBuilder? What I am trying to understand is for a multi-threaded application why is it required to go for a synchronous implementation (if the requirement doesn't state that).
Yes.
Very few applications actually concurrently modify a character sequence, with the result that there's almost no reason to use StringBuffer
.