Why use StringBuilder? StringBuffer can work with

2020-02-17 09:47发布

Suppose our application have only one thread. and we are using StringBuffer then what is the problem?

I mean if StringBuffer can handle multiple threads through synchronization, what is the problem to work with single thread?

Why use StringBuilder instead?

8条回答
一纸荒年 Trace。
2楼-- · 2020-02-17 10:41

Manish, although there is just one thread operating on your StringBuffer instance, there is some overhead in acquiring and releasing the monitor lock on the StringBuffer instance whenever any of its methods are invoked. Hence StringBuilder is a preferable choice in single thread environment.

查看更多
唯我独甜
3楼-- · 2020-02-17 10:43

StringBuilder is supposed to be a (tiny) bit faster because it isn't synchronised (thread safe).

You can notice the difference in really heavy applications.

The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html

查看更多
登录 后发表回答