What does “ StringBuilders are not thread-safe” mean?

前端 未结 5 1841
攒了一身酷
攒了一身酷 2020-12-31 12:51

I have read some articles about the pros and cons of String and StringBuilder in the Java Programming language. In one of the articles, the author

5条回答
  •  太阳男子
    2020-12-31 13:30

    If multiple threads are modifying the same instance of a StringBuilder, the result can be unexpected - i.e. some of the modifications may be lost. That's why you should use StringBuffer in such situations. If, however, each thread StringBuilder instance can be modified by only one thread, it is better to use StringBuilder, since it would be more efficient (thread safety comes with a performance cost).

提交回复
热议问题