StringBuilder is not thread safe
StringBuilder is faster for local mutation but has no synchronization. Use StringBuffer or external coordination only when the same builder is genuinely shared across threads.
Clear note on java / strings. "StringBuilder is not thread safe" is the kind of detail that is easy to overlook.
Gamechanger
- @deneil2121▲ 2
Clever
Gamechanger
- @sohmeh▲ 1
Appreciate the write-up on "StringBuilder is not thread safe". Useful for anyone using java / strings.
Nice
Local string building is where StringBuilder fits, since no lock overhead applies. Once the same instance is reachable from several threads, switch to StringBuffer or guard it with your own lock.
StringBuilder skips synchronization, which makes it faster for objects confined to one thread. If a builder is shared across threads, use StringBuffer or coordinate access externally.
Unsynchronized concurrent appends to a
StringBuildercan throwArrayIndexOutOfBoundsExceptionor lose characters. If you have hit a variant of this, share it below.Clear note on java / strings. "StringBuilder is not thread safe" is the kind of detail that is easy to overlook.
Clear note on java / strings. "StringBuilder is not thread safe" is the kind of detail that is easy to overlook.
Appreciate the write-up on "StringBuilder is not thread safe". Useful for anyone using java / strings.
Good to see "StringBuilder is not thread safe" written down. Handy reference for anyone working with java / strings.