“StringBuffer 是同步的(或线程安全的),而 StringBuilder 不是”,为什么这会使 StringBuffer 方法变慢?

发布于 2024-11-14 16:51:05 字数 208 浏览 7 评论 0原文

读完这篇文章后 - 'synchronized' 是什么意思? 我仍然无法理解为什么 StringBuffer 会在线程安全环境中比 StringBuilder 慢。 StringBuffer 必须做哪些额外的耗时工作才使其变慢?

After reading this - What does 'synchronized' mean? I was still unable to understand why StringBuffer would be slower than StringBuilder in a thread-safe environment. What extra time-consuming work does StringBuffer have to do that makes it slower?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风柔一江水 2024-11-21 16:51:05

即使获取和释放无竞争的锁,也会有一些小的开销,并且锁省略即使大多数实例不跨线程使用,它也不会在 StringBuffer 中工作,因为实例可以跨线程使用。

请参阅http://book.javanb.com/java-threads-3rd /jthreads3-CHP-5-SECT-1.html 了解 VM 在获取和释放锁时必须执行的操作的描述。

There is some small overhead acquiring and releasing even an uncontended lock, and lock elision won't work in StringBuffer even if most instances are not used cross-thread because an instance could be.

See http://book.javanb.com/java-threads-3rd/jthreads3-CHP-5-SECT-1.html for a description of what the VM has to do when acquiring and releasing locks.

久隐师 2024-11-21 16:51:05

确保一切同步运行。或者,更重要的是,不同步。同步方法调用意味着该方法的两次不同调用(如果该对象不是静态的,则在该对象上)必须轮流进入该方法。在线程 A(已在该方法中)完成之前,线程 B 无法进入方法synchMeth。

检查同步块是否已被锁定以及由谁锁定需要额外的时间。

Making sure things are running in synch. Or, more to the point, out of synch. Synchronizing a method call means two different invocations of that method (on that object if it is not static) have to take turns entering the method. Thread B cannot enter method synchMeth until Thread A (already in the method) has finished.

The checks for whether a synchronized block has been locked or not, and by whom, take extra time.

把人绕傻吧 2024-11-21 16:51:05

JavaDoc 阅读此内容

StringBuilder类提供了API
与 StringBuffer 兼容,但与
不保证同步。这
类被设计用作插入式
在某些地方替换 StringBuffer
使用字符串缓冲区的地方
通过单个线程(通常是
案)。在可能的情况下,它是
建议将该类用于
优先选择 StringBuffer
在大多数实现下速度更快。

您必须阅读这篇文章 StringBuffer 与 StringBuilder 性能比较< /a>

从 JDK 5 版本开始,此类已
并补充了同等的
专为单个人使用而设计的类
线程,{StringBuilder}。这
StringBuilder 类通常应该
优先使用这个,因为
它支持所有相同的操作
但它更快,因为它不执行
同步。

其他有用的链接:StringBuffer 和 StringBuilder 类之间的区别。

Read this from JavaDoc

StringBuilder class provides an API
compatible with StringBuffer, but with
no guarantee of synchronization. This
class is designed for use as a drop-in
replacement for StringBuffer in places
where the string buffer was being used
by a single thread (as is generally
the case). Where possible, it is
recommended that this class be used in
preference to StringBuffer as it will
be faster under most implementations.

You must read this article on StringBuffer vs. StringBuilder performance comparison

As of release JDK 5, this class has
been supplemented with an equivalent
class designed for use by a single
thread, {StringBuilder}. 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.

Additional Useful Link : Difference between StringBuffer and StringBuilder class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文